简体   繁体   中英

How to add buttons to a main window by programmatically in Qt and change styles them by use css file?

I will add two buttons to the a dialog and I want to change style sheets of them by use css file:

在此处输入图像描述

I also define the buttons as follows that didn't work:

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

If you are using the ID Selector QPushButton#objectName , be sure to also set the object name for your QPushButton, otherwise the selector will not work

sendButton = new QPushButton();
sendButton->setVisible(true);
sendButton->setText("sendButton");

sendButton->setObjectName("sendButton"); /* Set the object name */

sendButton->setStyleSheet(...);

Hope this helps =)

To add any widgets programmatically to a window (MainWindow, Dialog, ...) you have to do as follows:

  1. Create an instance of your widgets
  2. Create a layout (vertical, horizontal, or grid layout)
  3. Add widgets to the layout
  4. Set your window's layout

For example:

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

The output is as follows:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM