1

我已经意识到,将QToolButton添加到QToolBar时的工作方式与添加到 QFrame/QWidget 布局时的行为方式不同。

我需要在QToolButton上设置什么样式选项,因此它在QFrame中的行为与在QToolBar中(视觉上)完全相同?

我当前的平台是 OS X,这种行为差异在 Qt 4.8 和 5.1 上是相同的(根据我的测试)。

谢谢

例子:

QToolButton 行为差异

用于创建以下场景的代码:

int main(int argc, char **argv)
{
  QApplication app(argc,argv);

  QMainWindow* window    = new QMainWindow();
  QWidget* centralWidget = new QWidget();
  QVBoxLayout* layout    = new QVBoxLayout();

  centralWidget->setLayout(layout);
  window->setCentralWidget(centralWidget);
  window->setWindowTitle("QToolButton inside QToolBar vs QFrame/QWidget");

  layout->setContentsMargins(0, 0, 0, 0);
  layout->setSpacing(30);

  QToolBar* toolBar = new QToolBar();
  toolBar->setOrientation(Qt::Horizontal);
  layout->addWidget(toolBar);

  QToolButton* button_1 = new QToolButton();
  button_1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  button_1->setIcon(QPixmap(":/icons/icons/1.png"));
  button_1->setIconSize(QSize(32, 32));
  button_1->setText("Button 1");
  toolBar->addWidget(button_1);

  QToolButton* button_2 = new QToolButton();
  button_2->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  button_2->setIcon(QPixmap(":/icons/icons/2.png"));
  button_2->setIconSize(QSize(32, 32));
  button_2->setText("Button 2");
  toolBar->addWidget(button_2);

  QToolButton* button_3 = new QToolButton();
  button_3->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  button_3->setIcon(QPixmap(":/icons/icons/3.png"));
  button_3->setIconSize(QSize(32, 32));
  button_3->setText("Button 3");
  // toolBar->addWidget(button_3);

  QToolButton* button_4 = new QToolButton();
  button_4->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  button_4->setIcon(QPixmap(":/icons/icons/4.png"));
  button_4->setIconSize(QSize(32, 32));
  button_4->setText("Button 4");
  // toolBar->addWidget(button_4);

  QFrame* frame     = new QFrame();
  frame->setStyleSheet("QFrame { background: #ff0; }");
  layout->addWidget(frame);

  QHBoxLayout* frame_layout = new QHBoxLayout();
  frame_layout->setContentsMargins(0, 0, 0, 0);
  frame_layout->setSpacing(0);
  frame->setLayout(frame_layout);

  frame_layout->addWidget(button_4);
  frame_layout->addWidget(button_3);
  // button_4->animateClick(10000);
  frame_layout->addStretch();

  window->show();
  return app.exec();
}
4

1 回答 1

0

What you see is desired behavior and is coherent with visual behavior of other OS X applications, as far as I can tell. You may want to rethink whether what you're after is advisable. If you come up with a good reason, then I can look further into it.

于 2013-09-02T15:38:11.190 回答