简体   繁体   English

Qt QPushbutton 未通过 using.ui 添加

[英]Qt QPushbutton not added by using .ui

I'm trying to add new QPushbutton in my mainwindow.ui.我正在尝试在我的 mainwindow.ui 中添加新的 QPushbutton。 I added Qpushbutton by dragging in.ui, and in the right side I can see it added properly.我通过拖入.ui添加了Qpushbutton,在右侧我可以看到它添加正确。 在此处输入图像描述

(I added btnDownload button, and btnQuit is added long time ago. it works fine) (我添加了 btnDownload 按钮,btnQuit 是很久以前添加的。它工作正常)

I clicked go to slot on btnDownload and it created this code:我单击 go 插入 btnDownload 并创建了以下代码:


void MainWindow::on_btnDownload_clicked()
{
    QMessageBox::information(this,"notify","button is clicked "); //this part is what I added
}

however, when I build my project I don't see this button added in the place I located it.但是,当我构建我的项目时,我没有看到在我找到它的地方添加了这个按钮。 So to work around, I tried to use setGeometry function in mainwindow.cpp file to locate this button.因此,为了解决这个问题,我尝试在 mainwindow.cpp 文件中使用 setGeometry function 来定位此按钮。

this is a code I tried to wrote:这是我尝试编写的代码:

ui->btnDownload->setGeometry(x,y,wide,height);

but I get a warning message saying:但我收到一条警告消息:

no memeber named 'btnDownload' in Mainwindow::ui Mainwindow::ui 中没有名为“btnDownload”的成员

I did add QPushbutton in.ui, and created the connected function, but it cannot find this button.. how should I solve this?我确实在.ui中添加了QPushbutton,并创建了连接的function,但是找不到这个按钮..我应该怎么解决这个问题?

I think I fixed it.我想我修好了。 I compared it with btnQuit button (which works fine) and the difference was that the new button was not clarified in header file.我将它与 btnQuit 按钮(工作正常)进行了比较,不同之处在于 header 文件中没有说明新按钮。 I'm going to fix it and I think it will works.我要修复它,我认为它会起作用。 If it doesn't, I'll add it.如果没有,我会添加它。

Look into the.ui file.查看 .ui 文件。 It's XML.它是 XML。 You should see something like this你应该看到这样的东西

  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>

On the build directory typically at <workspace>\build-<project>-Desktop_Qt_6_2_0_MSVC2019_64bit-Debug\<project>_autogen\include you should find a file ui_mainwindow.h with the contents在通常位于<workspace>\build-<project>-Desktop_Qt_6_2_0_MSVC2019_64bit-Debug\<project>_autogen\include的构建目录中,您应该找到包含内容的文件ui_mainwindow.h

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QPushButton *pushButton; //<<======= this
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {

Right below you should find the instantiation of your button在下方,您应该找到按钮的实例化

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(800, 600);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        pushButton = new QPushButton(centralwidget);  // <<<============= this
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(130, 150, 75, 23));
        MainWindow->setCentralWidget(centralwidget);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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