title: QT Inherit from QTableWidgetItem to Widget and overide '<' operator
body: <p>I want a <code>QTableWidget</code> with certain cells as customized <code>QProgressBar</code>s, and I want it to be possible to sort the columns containing these.</p>
  
  <p>My customized <code>QProgressBar</code> is inheriting from both <code>QProgressBar</code> and <code>QTableWidgetItem</code>, and I am overiding the <code>'&lt;'</code> operator to allow for sorting.</p>
  
  <p>Below is an example of a customized <code>QTableWidget</code> (<code>QTableWidget_custom</code>), <code>QTableWidgetItem</code> (<code>QTableWidgetItem_double</code>) and <code>QProgressBar</code> (<code>QProgressBar_custom</code>). After the table is populated, I am only able to sort the columns containing <code>QTableWidgetItem_double</code>s.</p>
  
  <p><strong>QTableWidgetItem_double</strong></p>
  
  <pre><code>class QTableWidgetItem_double : public QTableWidgetItem
  {    
  public:
     QTableWidgetItem_double(const double _d = 0.0) :QTableWidgetItem(){
        d = _d;
        this-&gt;setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
        setText(QString::number(d, 'f', 4));
     }
  
     double d = 0.0;
  
     bool operator &lt;(const QTableWidgetItem&amp; other) const{
        QTableWidgetItem_double const *qtwd = dynamic_cast&lt;QTableWidgetItem_double const *&gt;(&amp;other);
        return d &lt; qtwd-&gt;d;
     }
  };
  </code></pre>
  
  <p><strong>QProgressBar_custom</strong></p>
  
  <pre><code>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 &lt;(const QTableWidgetItem&amp; other) const{
        QProgressBar const *p = dynamic_cast&lt;QProgressBar const*&gt;(&amp;other);
        return value() &lt; p-&gt;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&amp; txt){ txt1 = txt; }
     void setTxt2(const QString&amp; 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();
     }   
  };
  </code></pre>
  
  <p><strong>QTableWidget_custom</strong></p>
  
  <pre><code>class QTableWidget_custom : public QTableWidget
  {
  public:
     QTableWidget_custom(QWidget *parent) : QTableWidget(parent){}
  
     void addCustomProgressBarCell(int row, int col, double d1, double d2, const QString&amp; txt1, const QString&amp; txt2, double p){
        QProgressBar_custom* qprbar = new QProgressBar_custom();
        qprbar-&gt;setAmount1(d1);
        qprbar-&gt;setAmount2(d2);
        qprbar-&gt;setTxt1(txt1);
        qprbar-&gt;setTxt2(txt2);
        qprbar-&gt;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);
     }
  };
  </code></pre>
  
  <p><strong>Populating the table</strong></p>
  
  <pre><code>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-&gt;insertColumn(0); table-&gt;setColumnWidth(0, 70);
     table-&gt;insertColumn(1); table-&gt;setColumnWidth(1, 70);
     table-&gt;insertColumn(2);   table-&gt;setColumnWidth(2, 170);
     table-&gt;setSortingEnabled(false);
  
     //Add data to table:
     for (int i = 0; i &lt; 10; i++){
        table-&gt;insertRow(i);
  
        table-&gt;addCustomDoubleCell(i, 0, data1[i]);
        table-&gt;addCustomDoubleCell(i, 1, data2[i]);          
        table-&gt;addCustomProgressBarCell(i, 2, data3[i], data4[i], txt1[i], txt2[i], data4[i] / 100.0);
  
     }
     table-&gt;setSortingEnabled(true);    
  }
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <p><a href="https://i.stack.imgur.com/d3L1q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d3L1q.png" alt="Resulting table"></a></p>
  
  <p>If I click on the horizontal header of column 1 or 2, the table is sorted correctly using the overloaded operator of <code>QTableWidgetItem_double</code>. If I click on the third header, nothing happens. The overloaded function is never called.</p>