首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

QT 设立左上角按钮

2013-08-13 
QT 设置左上角按钮table-setStyleSheet(QTableCornerButton::section{background-color:red})?二. 设

QT 设置左上角按钮
table->setStyleSheet("QTableCornerButton::section{background-color:red;}");

?

二. 设置按钮文本

? ? 虽然没有提供直接的访问方式,可以通过findChild()来定位到该按钮,然后设置其文本及显示宽度.

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;    }};

??

? ? 运行效果:

? ??QT 设立左上角按钮

?

热点排行