【问题标题】:How to set precise size of QPushButton?如何设置 QPushButton 的精确尺寸?
【发布时间】:2016-07-03 22:50:33
【问题描述】:

我有 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)和“

附:我试过用setMinimumSize,但是没有效果。

附言此行 button->setStyleSheet("border: 1px solid black; background: white");给出这样的效果(精确的尺寸)

【问题讨论】:

    标签: c++ qt user-interface qt5


    【解决方案1】:
    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); (而不是调整大小),但它没有效果
    • 也许它可以工作,但由于样式的原因,它看起来不像 22x22。试试 66x66 或 11x11 尺寸,会不会看起来不一样?
    • 在 11x11 的情况下我有 9x9 尺寸,但在 66x66 的情况下我有 66x66(精确尺寸)
    • 在24x24的情况下,我有22x22的尺寸
    • 试试这个并再次测量:button->setStyleSheet("border: 1px solid black; background: white");
    【解决方案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,但没有效果)
    • QPushButton 上没有 setSize 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2017-07-15
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    相关资源
    最近更新 更多