如何设置QPushButton的精确大小?

10

我有一个SignatureEditorForm类,它继承自QDialog类:

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

它包含一个大小为(宽度:22,高度:22)且标题为“<”的QPushButton。

我有这个小部件(SignatureEditorForm)的图片。QPushButton的大小为(宽度:20,高度:20)。我该如何设置按钮的精确大小?

P.S. 我尝试使用setMinimumSize,但它没有效果。

enter image description here

P.P.S. 这行代码button->setStyleSheet("border: 1px solid black; background: white"); 会产生这样的效果(精确大小)。

enter image description here

2个回答

11
QWidget::setFixedSize  

这种方法以及其他方法都可以很好地设置QWidget的确切大小。

但是你的问题不在Qt中,而在于Windows样式。

小部件的实际大小是确切的,但Windows为动画保留边框空间,因此它看起来比你期望的要小。

要解决这个问题,您需要编写自己的样式,就像这样:

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

或者使用这里提供的任何样式:

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


我尝试了以下代码: button->setFixedSize(BUTTON_SIZE);(而不是resize),但没有任何效果。 - Alex Aparin
2
也许它能工作,但由于样式的原因看起来不像22x22。尝试使用66x66或11x11的大小,看起来会有所不同吗? - IGHOR
在24x24的情况下,我的尺寸为22x22。 - Alex Aparin
你如何衡量大小? - IGHOR
1
尝试这个并重新测量:button->setStyleSheet("border: 1px solid black; background: white"); - IGHOR
显示剩余5条评论

2
使用这个:

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

或者:

button->setFixedSize(BUTTON_SIZE);

我不知道setSize方法(我尝试使用setMinimumSize,但它没有任何效果). - Alex Aparin
QPushButton 没有 setSize 方法。 - zwcloud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接