Cclass CloseButton : public QAbstractButton{ Q_OBJECT public: CloseButton(QWidget* parent = 0); QSize sizeHint() const; inline QSize minimumSizeHint() const { return sizeHint(); } void changeEvent(QEvent* event); void paintEvent(QPaintEvent* event);}; CloseButton::CloseButton(QWidget* parent) : QAbstractButton(parent){ setFocusPolicy(Qt::NoFocus); setCursor(Qt::ArrowCursor); setToolTip(tr("Close")); QStyleOptionDockWidget opt; setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton, &opt)); resize(sizeHint());} QSize CloseButton::sizeHint() const{ return QSize(14, 14);} void CloseButton::paintEvent(QPaintEvent* event){ Q_UNUSED(event) QPainter painter(this); QStyleOption opt; opt.init(this); opt.state |= QStyle::State_AutoRaise; if(isEnabled() && underMouse() && !isChecked() && !isDown()) opt.state |= QStyle::State_Raised; if(isEnabled()) opt.state |= QStyle::State_Enabled; if(isChecked()) opt.state |= QStyle::State_On; if(isDown()) opt.state |= QStyle::State_Sunken; opt.state |= QStyle::State_Selected; QSize size = sizeHint(); QIcon::Mode mode = opt.state & QStyle::State_Enabled ? (opt.state & QStyle::State_Raised ? QIcon::Active : QIcon::Normal) : QIcon::Disabled; if(!(opt.state & QStyle::State_Raised) && !(opt.state & QStyle::State_Sunken) && !(opt.state & QStyle::State_Selected)) mode = QIcon::Disabled; QIcon::State state = opt.state & QStyle::State_On ? QIcon::On : QIcon::Off; QPixmap pixmap = icon().pixmap(size, mode, state); style()->drawItemPixmap(&painter, opt.rect, Qt::AlignCenter, pixmap);} void CloseButton::changeEvent(QEvent* event){ QAbstractButton::changeEvent(event); if(event->type() == QEvent::StyleChange) { QStyleOptionDockWidget opt; setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton, &opt)); }}
closebutton=new QToolButton();closebutton->setIcon(qApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
int w=closebutton->geometry().width();int h=closebutton->geometry().height();qDebug() << "width " << w << " height " << h;
closebutton=new QToolButton(); closebutton->setVisible(true); int w=closebutton->geometry().width(); int h=closebutton->geometry().height(); qDebug() << "width " << w << " height " << h;
closebutton=new QToolButton(this);