Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177027
  • 博文数量: 22
  • 博客积分: 1411
  • 博客等级: 上尉
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-19 18:12
文章分类
文章存档

2011年(1)

2010年(1)

2008年(20)

我的朋友

分类:

2008-04-30 13:12:37

How to set the most top-left corner of a QTableWidget?  QTableWidget NW corner header item?  and QTableWidget NW corner header item?

 

QTableView's private cornerWidget member is the QTableCornerButton, and it oddly hides another member of the same name down the inheritance chain, namely QAbstractScrollArea::cornerWidget. Only the latter is exposed via the method cornerWidget(). Doh. QTableWidget::cornerWidget() returns not the QTableCornerButton instance, but QAbstractScrollArea::cornerWidget, which is understandably NULL. Wish there was a QTableView::cornerButton() method.

It's still possible with some rather ugly "hacking". One would need to use for example QObject::findChild() to get access to the private corner button. An event filter is required for "overriding" the button's paintEvent() (QTableCornerButton::paintEvent() doesn't paint the text even if the button had text set).

 

----------------------------------------snippet--------------------------------------------------------------------

 

bool Pci1716CalibrationForm::eventFilter(QObject* o, QEvent* e)
{
     if (e->type() == QEvent::Paint)
     {
     QAbstractButton* btn = qobject_cast<QAbstractButton*>(o);
     if (btn) {
     printf("paint\n");
     // paint by hand (borrowed from QTableCornerButton)

     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.textAlignment = Qt::AlignCenter;
     opt.state = state;
     opt.rect = btn->rect();
     opt.text = btn->text(); // this line is the only difference to QTableCornerButton

     opt.position = QStyleOptionHeader::OnlyOneSection;
     QStylePainter painter(btn);
     painter.drawControl(QStyle::CE_Header, opt);
     return true; // eat event

     }
     }
     return false;
}

void Pci1716CalibrationForm::initializeTableWidgetCornerButton(QTableWidget *tableWidget, QString text)
{
     QAbstractButton *btn = tableWidget->findChild<QAbstractButton*>();
     if (btn) {
     btn->setText(text);
     btn->setStyleSheet("QWidget {background-color:blue}");
     btn->installEventFilter(this);

     // adjust the width of the vertical header to match the preferred corner button width

     // (unfortunately QAbstractButton doesn't implement any size hinting functionality)

     QStyleOptionHeader opt;
     opt.text = btn->text();
     QSize s = (btn->style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), btn).expandedTo(QApplication::globalStrut()));
     if (s.isValid()) {
     tableWidget->verticalHeader()->setMinimumWidth(s.width());
     }
    
     }

}

----------------------------------------snippet--------------------------------------------------------------------

 

Here is an example code.

#include <QtGui>

class TableWidget : public QTableWidget
{
public:
    TableWidget(int rows, int cols, QWidget* parent = 0)
        : QTableWidget(rows, cols, parent)
    {
        QAbstractButton* btn = findChild<QAbstractButton*>();
        if (btn)
        {
            btn->setText("Text");
            btn->installEventFilter(this);

            // adjust the width of the vertical header to match the preferred corner button width

            // (unfortunately QAbstractButton doesn't implement any size hinting functionality)

            QStyleOptionHeader opt;
            opt.text = btn->text();
            QSize s = (btn->style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), btn).
                expandedTo(QApplication::globalStrut()));
            if (s.isValid())
                verticalHeader()->setMinimumWidth(s.width());
        }
    }

    bool eventFilter(QObject* o, QEvent* e)
    {
        if (e->type() == QEvent::Paint)
        {
            QAbstractButton* btn = qobject_cast<QAbstractButton*>(o);
            if (btn)
            {
                // paint by hand (borrowed from QTableCornerButton)

                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(); // this line is the only difference to QTableCornerButton

                opt.position = QStyleOptionHeader::OnlyOneSection;
                QStylePainter painter(btn);
                painter.drawControl(QStyle::CE_Header, opt);
                return true; // eat event

            }
        }
        return false;
    }
};

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    TableWidget t(2, 2);
    t.show();
    return a.exec();
}

阅读(7216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~