10

I have SignatureEditorForm class, which inherits QDialog class:

SignatureEditorForm(QWidget* parent_widget) : QDialog(parent_widget)
{
    resize(SIGNATURE_EDITOR_SIZE);
    setWindowTitle(QString("Signature Editor"));
    setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowCloseButtonHint);
    setModal(true);
    {
        QPushButton* const button = new QPushButton(QString("<"), this);
        const QSize BUTTON_SIZE = QSize(22, 22);
        button->resize(BUTTON_SIZE);
    }
}

It contains QPushButton which has size (width: 22, height: 22) and "<" caption. I have such picture of this widget (SignatureEditorForm). QPushButton has size (width: 20, height: 20). How can I set precise size of button?

P.S. I tried to use setMinimumSize, but it has not effects.

enter image description here

P.P.S. this line button->setStyleSheet("border: 1px solid black; background: white"); gives such effect (precise size)

enter image description here

2 Answers 2

11
QWidget::setFixedSize  

This and other methods works fine to set exact size of QWidget.

But your problem is not in Qt, it in Windows Styling.

Actual size of widget is exact, but Windows reserve space on borders for animations, so it looks smaller than you expect.

To fix it you need to write own style like this:

button->setStyleSheet("border: 1px solid black; background: white");

Or use any of styles available here:

http://doc.qt.io/qt-5/gallery.html

10
  • I tried such code : button->setFixedSize(BUTTON_SIZE); (instead of resize), but it has not effect
    – LmTinyToon
    Mar 17, 2016 at 14:22
  • 2
    Maybe it works but looks visual not like 22x22 because of Styles. Try 66x66 or 11x11 sizes, is it looks different?
    – IGHOR
    Mar 17, 2016 at 14:25
  • In the case of 11x11 I have 9x9 size, but in the case 66x66 I have 66x66 (precise size)
    – LmTinyToon
    Mar 17, 2016 at 14:28
  • In the case of 24x24, I have 22x22 size
    – LmTinyToon
    Mar 17, 2016 at 14:30
  • 1
    Try this and measure again: button->setStyleSheet("border: 1px solid black; background: white");
    – IGHOR
    Mar 17, 2016 at 14:35
1

Use this:

QPushButton* const button = new QPushButton(QString("<"), this); 
const QSize BUTTON_SIZE = QSize(22, 22);
button->setMinimumSize(BUTTON_SIZE);

or:

button->setFixedSize(BUTTON_SIZE);
2
  • I dont know about setSize method (I tried use setMinimumSize, but it has not effects)
    – LmTinyToon
    Mar 17, 2016 at 14:34
  • There is no setSize method on QPushButton.
    – zwcloud
    Sep 6, 2018 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.