You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, March 26th 2014, 2:15pm

What's the better way to make closable QPushButton?

That is something like closable tabs in QTabBar. I made this way:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
BtnWithClosing::BtnWithClosing(QWidget *parent): QFrame(parent) {
QHBoxLayout *layout = new QHBoxLayout;
layout->setMargin(4);
layout->setSpacing(0);
layout->setSizeConstraint(QLayout::SetMaximumSize);
setStyleSheet("...");
QPushButton *btnOk = new QPushButton(":/images/saveandexit.png");
QPushButton *btnClose = new QPushButton(QIcon(":/images/closebtnover.png"), "");
btnClose->setStyleSheet("...");
btnOk->setStyleSheet("...");
layout->addWidget(btnOk);
layout->addWidget(btnClose);
setLayout(layout);
}

but it looks very bad. I don't know how to fill the frame with buttons entirely so to make it look like one solid button. And I want close button look small and round. So I think only way to paint icon on QPushButton and catch mouse events. How can I do this? Thanx for any reference!
Thanks in advance!

2

Wednesday, March 26th 2014, 4:15pm

Ready:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BtnWithClosing::BtnWithClosing(QString txt, QWidget *parent):
QPushButton(txt, parent) {
  QHBoxLayout *layout = new QHBoxLayout;
  layout->setMargin(4);
  layout->setSpacing(0);
  layout->setSizeConstraint(QLayout::SetMaximumSize);
  setText(text() + "    "); // for close button space
  QPushButton *btnClose = new QPushButton;
  btnClose->setStyleSheet("QPushButton:hover {image:"
    "url(:/images/closebtnover.png);}"
    "QPushButton {image: url(:/images/closebtn.png); margin-right: -9;"
    "margin-left: -6; min-width: 0; background-color: transparent;"
    "min-height: 0; border-width: 0px; border-radius: 9px; padding: 0px;}");
  layout->addWidget(btnClose, 0, Qt::AlignRight);
  setLayout(layout);
}

Similar threads

Used tags

QT 4.7