这有点令人沮丧,但是我找不到设置QPushButton大小的正确方法。根据我的阅读,我认为这段代码应该可以工作:

// SEND BUTTON
sendButton = new QPushButton(this);
sendButton->setText("Send");
sendButton->setMinimumSize(QSize(0,0));
sendButton->setMaximumSize(QSize(10000,10000));
sendButton->setGeometry(QRect(QPoint(30, 100),QSize(10, 20)));
connect(sendButton, &QPushButton::released, this, &MainWindow::handleSendButton);


// CENTRAL WIDGET
QWidget* centralWidget = new QWidget(this);
centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

// LAYOUT
QGridLayout* layout = new QGridLayout(centralWidget);
layout->addWidget(sendButton,0,0,0,0);

但是按钮始终会扩展到整个应用程序窗口。能否请你帮忙?

最佳答案

窗口小部件默认具有的策略是QSizePolicy::Preferred,但是在您的情况下,您应该使用QSizePolicy::Fixed



另外,您必须建立要居中的路线,以便从上面获得以下内容:

sendButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(sendButton,0,0,0,0, Qt::AlignCenter);

关于c++ - Qt5:QPushButon大小设置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46193762/

10-11 17:05