QT Inherit from QTableWidgetItem to Widget and overide '<' operator

混江龙づ霸主 提交于 2019-12-22 20:44:13

问题


I want a QTableWidget with certain cells as customized QProgressBars, and I want it to be possible to sort the columns containing these.

My customized QProgressBar is inheriting from both QProgressBar and QTableWidgetItem, and I am overiding the '<' operator to allow for sorting.

Below is an example of a customized QTableWidget (QTableWidget_custom), QTableWidgetItem (QTableWidgetItem_double) and QProgressBar (QProgressBar_custom). After the table is populated, I am only able to sort the columns containing QTableWidgetItem_doubles.

QTableWidgetItem_double

class QTableWidgetItem_double : public QTableWidgetItem
{    
public:
   QTableWidgetItem_double(const double _d = 0.0) :QTableWidgetItem(){
      d = _d;
      this->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
      setText(QString::number(d, 'f', 4));
   }

   double d = 0.0;

   bool operator <(const QTableWidgetItem& other) const{
      QTableWidgetItem_double const *qtwd = dynamic_cast<QTableWidgetItem_double const *>(&other);
      return d < qtwd->d;
   }
};

QProgressBar_custom

class QProgressBar_custom : public QProgressBar, public QTableWidgetItem
{    
public:
   QProgressBar_custom() : QProgressBar(){
      setTextVisible(true);
      setStyleSheet(" QProgressBar { border: 1px solid grey; border-radius: 0px; text-align: right;} QProgressBar::chunk { background-color: rgba(0,255,0,100); width: 1px;}");
      setAlignment(Qt::AlignCenter);
   }    
   QProgressBar_custom(const QString txt) : QTableWidgetItem(txt){}

   bool operator <(const QTableWidgetItem& other) const{
      QProgressBar const *p = dynamic_cast<QProgressBar const*>(&other);
      return value() < p->value();
   }

private:
   double d1 = 0.0;
   double d2 = 0.0;
   QString txt1 = "";
   QString txt2 = "";    
public:
   void setText(){
      QString txtfield = QString::number(d1, 'f', 2) + " " + txt1 + " \t" + QString::number(d2, 'f', 4) + " " + txt2 + " ";
      setFormat(txtfield);
   }    
   void setTxt1(const QString& txt){ txt1 = txt; }
   void setTxt2(const QString& txt){ txt2 = txt; }
   void setAmount1(double d){ d1 = d; }
   void setAmount2(double d){ d2 = d; }
   void setPercentage(double p){
      setValue(int(100.0*p));
      setText();
   }   
};

QTableWidget_custom

class QTableWidget_custom : public QTableWidget
{
public:
   QTableWidget_custom(QWidget *parent) : QTableWidget(parent){}

   void addCustomProgressBarCell(int row, int col, double d1, double d2, const QString& txt1, const QString& txt2, double p){
      QProgressBar_custom* qprbar = new QProgressBar_custom();
      qprbar->setAmount1(d1);
      qprbar->setAmount2(d2);
      qprbar->setTxt1(txt1);
      qprbar->setTxt2(txt2);
      qprbar->setPercentage(p);
      setCellWidget(row, col, qprbar);
   }
   void addCustomDoubleCell(int row, int col, double d){
      QTableWidgetItem_double* qtwd = new QTableWidgetItem_double(d);
      setItem(row, col, qtwd);
   }
};

Populating the table

void QTWidgetTableItem::testCustomTableWidgetItem(){        
   //Some random data:
   double data1[10] = { 1.0, 2.0, 34.4, 32.02, 1000.0, 2002.0, 0.0001, 0.02, 0.009, 10.0 };
   double data2[10] = { -1.01, 22.3, 54.4, 1232.02, 0.0, -22.0, 0.1231, -0.02, 10.009, 5.0 };
   double data3[10] = { 5.0, 201.0, 434.4444, 32.32, 1234.231, 2002.232, 0.111, 0.0422, 0.212, -10.0 };
   double data4[10] = { 1.0, 2.0, 5.0, 25.00, 12.2, 100.0, 0.0, 50.0, 65.03, 0.9 };
   QString txt1[10] = { "abc", "abc", "abc", "eur", "usd", "php", "cpp", "xxx", "yyy", "zzz" };
   QString txt2[10] = { "aaa", "bbb", "ccc", "ddd", "asd", "qwe", "trt", "tr1", "tr2", "tr3" };

   //Prepare table:
   QTableWidget_custom* table = ui.tableWidget;
   table->insertColumn(0); table->setColumnWidth(0, 70);
   table->insertColumn(1); table->setColumnWidth(1, 70);
   table->insertColumn(2);   table->setColumnWidth(2, 170);
   table->setSortingEnabled(false);

   //Add data to table:
   for (int i = 0; i < 10; i++){
      table->insertRow(i);

      table->addCustomDoubleCell(i, 0, data1[i]);
      table->addCustomDoubleCell(i, 1, data2[i]);          
      table->addCustomProgressBarCell(i, 2, data3[i], data4[i], txt1[i], txt2[i], data4[i] / 100.0);

   }
   table->setSortingEnabled(true);    
}

Result

If I click on the horizontal header of column 1 or 2, the table is sorted correctly using the overloaded operator of QTableWidgetItem_double. If I click on the third header, nothing happens. The overloaded function is never called.


回答1:


Only the QTableWidgetItem are called when ordering them, in your case the QProgressBar_custom are added as cellWidgets, but not as QTableWidgetItems, you must do it as follows:

void addCustomProgressBarCell(int row, int col, double d1, double d2, const QString& txt1, const QString& txt2, double p){
    QProgressBar_custom *qprbar = new QProgressBar_custom;
    qprbar->setAmount1(d1);
    qprbar->setAmount2(d2);
    qprbar->setTxt1(txt1);
    qprbar->setTxt2(txt2);
    qprbar->setPercentage(p);
    setCellWidget(row, col, qprbar);
    setItem(row, col, qprbar); // add this line
}




回答2:


Some comments:

  • If there are many items, using a delegate will be more performant than instantiating widgets for every item. The delegate's paint method can use QStyle::drawControl. A complete example will be provided later. It is not particularly complicated, and works very well.

  • Prefer QString format strings to concatenation, for performance and readability. Use QStringLiteral instead of casting C strings.

    auto const txtField = QStringLiteral("%1 %2 \t%3 %4 ")
                          .arg(d1, 'f', 2) .arg(txt1)
                          .arg(f2, 'f', 4) .arg(txt2);
    
  • For QObject-derived classes, qobject_cast is another option. To make it robust and not potentially break internal data structures in the table view, the comparison operator must always return a consistent result (or no result at all - i.e. assert):

    bool operator <(const QTableWidgetItem& other) const {
       auto const *p = qobject_cast<const QProgressBar*>(&other);
       if (true)
          // let's always succeed
          return p ? value() < p->value() 
                   : static_cast<const QTableWidgetItem*>(this) < &other;
       else {
          // catch errors
          Q_ASSERT(p);
          return value() < p->value();
       }
    }
    


来源:https://stackoverflow.com/questions/49416179/qt-inherit-from-qtablewidgetitem-to-widget-and-overide-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!