Tables

Lingfa Yang

Our TableWidget is a subclass of QTableWidget. Beyond the standard table display facilities for applications, our TableWidget accepts html-format table or tabbed plain text as two convenient inputs.
  1. Standard html table format:

    <table>...</table> - define a table
    <th> - headers
    <tr> - row
    <td> - column
    
    Here is an example. Our TableWidget rendered as this:
    Mozilla browser rendered as this:
  2. Tabbed plain text format

    If you are lazy to write these tags, you can choose plain text format, where use tab-key for next column and line-break for next row. Our TableWidget will give the same rendering result.
    bool TableWidget::setTabText(const QString &text)
    {
        QStringList li(text.split("\n"));
        int rows = li.size();
        int cols = li[0].split("\t", QString::SkipEmptyParts).size();
        this->setColumnCount(cols);
        this->setRowCount(rows);
    
        QTableWidgetItem *item;
        for(int row=0; row<rows; row ++)
        {
           QStringList lineTxt = li[row].split("\t",QString::SkipEmptyParts);
           for(int col=0; col<cols; col++)
           {
              item = new QTableWidgetItem;
              item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
              this->setItem(row, col, item);
    
              if(col < lineTxt.size()) // have text provided
                  this->item(row, col)->setText(lineTxt[col]);
           }
        }
        return true;
    }
    
  3. How to access Left-top Corner widget?

    This is a challenge question if you want to put a text string to the left-top corner. Qt has no simple solution. Use even filter it can be done this ugly way:
    bool TableWidget::eventFilter(QObject* o, QEvent* e)
    {
        if (e->type() == QEvent::Paint)
        {
            QAbstractButton* btn = qobject_cast<QAbstractButton*>(o);
            if (btn)
            {
               // self paint
               QStyleOptionHeader opt;
               opt.init(btn);
               QStyle::State state = QStyle::State_None;
               if (btn->isEnabled())
                       state |= QStyle::State_Enabled;
               if (btn->isActiveWindow())
                       state |= QStyle::State_Active;
               if (btn->isDown())
                       state |= QStyle::State_Sunken;
               opt.state = state;
               opt.rect = btn->rect();
               opt.text = btn->text(); // !!!
               opt.position = QStyleOptionHeader::OnlyOneSection;
               QStylePainter painter(btn);
               painter.drawControl(QStyle::CE_Header, opt);
               return true; // eat event
            }
        }
        return false;
    }
    
  4. ActiveX table embedding

    Want embedding table widget into Office documents or IE-browsed web pages? We can use QAxFactory, turn the table widget into an activeX control.

    A demo shows an active table in PowerPoint 2007 which has persist data


    A demo shows an active table in IE.

Dialogs | Model/View( List/Tree, Table, Network) | XML/ Office 2007 XML | COM/ActiveX | Network TCP/IP | OpenGL | Online Help ... || MyQt | Qt C++ | C/C++ | eBooks