如何在 Qt 中以编程方式将按钮添加到主 window 并使用 css 文件更改它们的样式?
How to add buttons to a main window by programmatically in Qt and change styles them by use css file?
我将向对话框添加两个按钮,我想通过使用 css 文件来更改它们的样式表:
我还按如下方式定义了不起作用的按钮:
sendButton = new QPushButton();
sendButton->setVisible(true);
sendButton->setText("sendButton");
sendButton->setStyleSheet("QPushButton#sendButton {\n"
"background-color: red;\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 10px;\n"
"border-color: beige;\n"
"font: bold 14px;\n"
"min-width: 10em;\n"
"padding: 6px;\n"
"}\n"
"QPushButton#reciveButton {\n"
"background-color: green;\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 10px;\n"
"border-color: beige;\n"
"font: bold 14px;\n"
"min-width: 10em;\n"
"padding: 6px;\n"
"}\n");
ui->horizontalLayout->addWidget(sendButton);
如果您使用的是ID选择器 QPushButton#objectName
,请务必同时为您的QPushButton设置对象名称,否则选择器将不起作用
sendButton = new QPushButton();
sendButton->setVisible(true);
sendButton->setText("sendButton");
sendButton->setObjectName("sendButton"); /* Set the object name */
sendButton->setStyleSheet(...);
希望这对您有所帮助 =)
要以编程方式将任何小部件添加到 window(MainWindow、Dialog 等),您必须执行以下操作:
- 创建小部件实例
- 创建布局(垂直、水平或网格布局)
- 将小部件添加到布局
- 设置 window 的布局
例如:
//Step 1: create widgets
QPushButton *sendbtn = new QPushButton("sendButton");
sendbtn->setObjectName("mySendButton");
QPushButton *receivebtn = new QPushButton("receiveButton");
receivebtn->setObjectName("myReceiveButton");
//Step 2: create a layout
QHBoxLayout *hlayout = new QHBoxLayout;
//Step 3: add widgets to the layout
hlayout->addWidget(sendbtn);
hlayout->addWidget(receivebtn);
QWidget *w = new QWidget;
w->setLayout(hlayout);
setCentralWidget(w);
setStyleSheet("QPushButton#mySendButton"
"{"
"background-color: red;"
"border-style: outset;"
"border-width: 2px;"
"border-radius: 10px;"
"border-color: beige;"
"font: bold 14px;"
"min-width: 10em;"
"padding: 6px;"
"}"
"QPushButton#myReceiveButton"
"{"
"background-color: green;"
"border-style: outset;"
"border-width: 2px;"
"border-radius: 10px;"
"border-color: beige;"
"font: bold 14px;"
"min-width: 10em;"
"padding: 6px;"
"}");
输出结果如下:
我将向对话框添加两个按钮,我想通过使用 css 文件来更改它们的样式表:
我还按如下方式定义了不起作用的按钮:
sendButton = new QPushButton();
sendButton->setVisible(true);
sendButton->setText("sendButton");
sendButton->setStyleSheet("QPushButton#sendButton {\n"
"background-color: red;\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 10px;\n"
"border-color: beige;\n"
"font: bold 14px;\n"
"min-width: 10em;\n"
"padding: 6px;\n"
"}\n"
"QPushButton#reciveButton {\n"
"background-color: green;\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 10px;\n"
"border-color: beige;\n"
"font: bold 14px;\n"
"min-width: 10em;\n"
"padding: 6px;\n"
"}\n");
ui->horizontalLayout->addWidget(sendButton);
如果您使用的是ID选择器 QPushButton#objectName
,请务必同时为您的QPushButton设置对象名称,否则选择器将不起作用
sendButton = new QPushButton();
sendButton->setVisible(true);
sendButton->setText("sendButton");
sendButton->setObjectName("sendButton"); /* Set the object name */
sendButton->setStyleSheet(...);
希望这对您有所帮助 =)
要以编程方式将任何小部件添加到 window(MainWindow、Dialog 等),您必须执行以下操作:
- 创建小部件实例
- 创建布局(垂直、水平或网格布局)
- 将小部件添加到布局
- 设置 window 的布局
例如:
//Step 1: create widgets
QPushButton *sendbtn = new QPushButton("sendButton");
sendbtn->setObjectName("mySendButton");
QPushButton *receivebtn = new QPushButton("receiveButton");
receivebtn->setObjectName("myReceiveButton");
//Step 2: create a layout
QHBoxLayout *hlayout = new QHBoxLayout;
//Step 3: add widgets to the layout
hlayout->addWidget(sendbtn);
hlayout->addWidget(receivebtn);
QWidget *w = new QWidget;
w->setLayout(hlayout);
setCentralWidget(w);
setStyleSheet("QPushButton#mySendButton"
"{"
"background-color: red;"
"border-style: outset;"
"border-width: 2px;"
"border-radius: 10px;"
"border-color: beige;"
"font: bold 14px;"
"min-width: 10em;"
"padding: 6px;"
"}"
"QPushButton#myReceiveButton"
"{"
"background-color: green;"
"border-style: outset;"
"border-width: 2px;"
"border-radius: 10px;"
"border-color: beige;"
"font: bold 14px;"
"min-width: 10em;"
"padding: 6px;"
"}");
输出结果如下: