Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProgressBar inside QTableView column (Size problem)
QtWS25 Last Chance

QProgressBar inside QTableView column (Size problem)

Scheduled Pinned Locked Moved Solved General and Desktop
qprogressqprogressbarqtableview c++custom model
4 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Proton Phoenix
    wrote on last edited by Proton Phoenix
    #1

    Hi `~ I Have A Size problem on progressbar inside QTableView (Table is from Right To left)
    the progressbar takes the size of it's column 1 and other column 0 which is not a linked with it

    • it contains headers TEXT.. i just clean it with paint
    • when i click to column 0 (from the right to left) it remove the progressbar and when i click something else it comes back as the picture
      here's my code
    expiredatesDelegate::expiredatesDelegate(QObject *parent)
        : QStyledItemDelegate{parent}
    {
    
    }
    void expiredatesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
    {
        if (index.column() == 1) {
            int progress = index.data().toInt();
            QStyleOptionProgressBar progressBarOption;
            progressBarOption.rect = option.rect;
            progressBarOption.state = QStyle::StateFlag::State_Horizontal;
            progressBarOption.minimum = 0;
            progressBarOption.maximum = 100;
            progressBarOption.progress = progress;
            progressBarOption.text = QString::number(progress) + "%";
            progressBarOption.textVisible = true;
            progressBarOption.textAlignment =Qt::AlignCenter;
            painter->setFont(QFont("Arial",10));
            QApplication::style()->drawControl(QStyle::CE_ProgressBar,
                                               &progressBarOption, painter);
        } else
            QStyledItemDelegate::paint(painter, option, index);
            
    }
    
    ui->tableView->setModel(expiredatesmodel);
        ui->tableView->setColumnWidth(0,260);
        ui->tableView->setColumnWidth(1,260);
        ui->tableView->setColumnWidth(2,170);
        ui->tableView->setColumnWidth(3,170);
    /****/*
       ui->tableView->model()->setData(index,productname[i],1);
        ui->tableView->setItemDelegate(new expiredatesDelegate());
    

    d9af387d-cce6-474e-a0de-6b9ff3a3441e-image.png

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Try this:

      • instead of ui->tableView->setItemDelegate(new expiredatesDelegate()); call ui->tableView->setItemDelegateForColumn(1,new expiredatesDelegate());
      • change paint to:
      void expiredatesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
      {
      QStyleOptionViewItem opt = option;
          initStyleOption(&opt, index);
              const int progress = index.data().toInt();
              QStyleOptionProgressBar progressBarOption;
              progressBarOption.rect = opt.rect;
              progressBarOption.direction = opt.direction;
              progressBarOption.fontMetrics= opt.fontMetrics;
              progressBarOption.palette= opt.palette;
              progressBarOption.state = opt.state | QStyle::State_Horizontal;
              progressBarOption.styleObject = opt.styleObject;
              progressBarOption.minimum = 0;
              progressBarOption.maximum = 100;
              progressBarOption.progress = progress;
              progressBarOption.text = tr("%1%").arg(progress);
              progressBarOption.textVisible = true;
              progressBarOption.textAlignment =Qt::AlignCenter;
      painter->save();
              painter->setFont(QFont("Arial",10));
              (opt.widget ?  opt.widget.style : QApplication::style())->drawControl(QStyle::CE_ProgressBar,
                                                 &progressBarOption, painter);
      painter->restore();
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      P 1 Reply Last reply
      2
      • P Offline
        P Offline
        Proton Phoenix
        wrote on last edited by Proton Phoenix
        #3

        @VRonin said in QProgressBar inside QTableView column (Size problem):

        widget

        Hi Thank you so much bro
        i tried it but almost the same
        only it changes it's direction

        using ui->tableView->setItemDelegateForColumn(1,new expiredatesDelegate());
        

        it looks like this
        36285af0-bf8f-4796-844b-c44c1e848406-image.png

        using

        ui->tableView->setItemDelegate(new expiredatesDelegate());
        

        70e30f0c-9e99-42d8-a212-92ded415a0f5-image.png
        but it got it's right size but i lost other columns ^^ i am sure there's a very close solution now ~~ Where it is ^^?

        1 Reply Last reply
        0
        • VRoninV VRonin

          Try this:

          • instead of ui->tableView->setItemDelegate(new expiredatesDelegate()); call ui->tableView->setItemDelegateForColumn(1,new expiredatesDelegate());
          • change paint to:
          void expiredatesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const
          {
          QStyleOptionViewItem opt = option;
              initStyleOption(&opt, index);
                  const int progress = index.data().toInt();
                  QStyleOptionProgressBar progressBarOption;
                  progressBarOption.rect = opt.rect;
                  progressBarOption.direction = opt.direction;
                  progressBarOption.fontMetrics= opt.fontMetrics;
                  progressBarOption.palette= opt.palette;
                  progressBarOption.state = opt.state | QStyle::State_Horizontal;
                  progressBarOption.styleObject = opt.styleObject;
                  progressBarOption.minimum = 0;
                  progressBarOption.maximum = 100;
                  progressBarOption.progress = progress;
                  progressBarOption.text = tr("%1%").arg(progress);
                  progressBarOption.textVisible = true;
                  progressBarOption.textAlignment =Qt::AlignCenter;
          painter->save();
                  painter->setFont(QFont("Arial",10));
                  (opt.widget ?  opt.widget.style : QApplication::style())->drawControl(QStyle::CE_ProgressBar,
                                                     &progressBarOption, painter);
          painter->restore();
          }
          
          P Offline
          P Offline
          Proton Phoenix
          wrote on last edited by
          #4

          @VRonin
          you knew what bro :D ^^ when the model gets data there's a values higher than the max of the progressbar that's why it takes the size of other columns ... when i set higher progressBarOption.maximum = 100;
          problem solved
          Thank you so much
          without you i coudn't find it even if it was that easy

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved