Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Qt中通过编程向主窗口添加按钮,并使用css文件更改它们的样式?_C++_Qt_Qwidget - Fatal编程技术网

C++ 如何在Qt中通过编程向主窗口添加按钮,并使用css文件更改它们的样式?

C++ 如何在Qt中通过编程向主窗口添加按钮,并使用css文件更改它们的样式?,c++,qt,qwidget,C++,Qt,Qwidget,我将向a对话框添加两个按钮,并希望使用css文件更改它们的样式表: 我还将不起作用的按钮定义如下: sendButton = new QPushButton(); sendButton->setVisible(true); sendButton->setText("sendButton"); sendButton->setStyleSheet("QPushButton#sendButton {\n" "background-

我将向a对话框添加两个按钮,并希望使用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=新的QPushButton();
sendButton->setVisible(真);
sendButton->setText(“sendButton”);
sendButton->setObjectName(“sendButton”);/*设置对象名称*/
sendButton->setStyleSheet(…);

希望这有助于=)

要以编程方式将任何小部件添加到窗口(主窗口、对话框等),您必须执行以下操作:

  • 创建小部件的实例
  • 创建布局(垂直、水平或栅格布局)
  • 将小部件添加到布局中
  • 设置窗口的布局
  • 例如:

        //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;"
                         "}");
    
    结果如下:


    当我按如下方式定义按钮时,此方法不起作用:sendButton=new QPushButton();sendButton->setVisible(真);sendButton->setText(“sendButton”);ui->horizontalLayout->addWidget(发送按钮);我已经编辑了我的答案以匹配您的用例。当你设置一个小部件的样式表时,现在应该可以了(可能=),一旦Qt到达该命令行,如果样式表命令无效(即,它不是一个有效的css样式表),Qt将在
    应用程序输出中写入错误。请检查您的样式表是否有效。我对
    font:bold 14px有点犹豫。因为我是从css文件中读取此样式,所以ID选择器必须清晰。#sendButtonOk@MohsenGhahremaniManesh。请看我的编辑1。希望有帮助:)你能在这里写下错误或警告信息吗?我认为
    setLayout
    行有一些问题。因为
    main窗口
    已经有一个布局,我们正在尝试替换它。老实说,我现在无法使用我的笔记本电脑。我会检查一下,一小时后回复。@MohsenGhahremaniManesh我发布了最终解决方案。这在我的机器上很好用。问题是,在使用ID选择器之前,必须为该对象设置对象名。请参阅以了解更多详细信息。如果这个答案能解决你的问题,请接受。