C++ – How to set precise size of QPushButton

cqtqt5user interface

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

Best Answer

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