【问题标题】:How can I call the "pushButton" in main.cpp?如何在 main.cpp 中调用“pushButton”?
【发布时间】:2012-10-07 08:40:07
【问题描述】:

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QObject::connect(pushButton, SIGNAL(clicked()),
            &a, SLOT(quit()));

    return a.exec();
}

上面给出了所有代码。在一般的 Qt GUI 程序中,我在 UI 窗体上放置了一个 pushButton,并尝试在 main.cpp 中使用它。但出现以下错误:

main.cpp:10: Error:'pushButton' was not declared in this scope

你能给我一个解决方案吗?如何在 main.cpp 中调用它? 谢谢!

补充1

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QObject::connect(ui->pushButton, SIGNAL(clicked()),
            QCoreApplication::instance(), SLOT(close()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

如果我这样做,那么程序可以运行但不能关闭整个应用程序。我猜这是因为 QCoreApplication::instance() 由于在构造函数阶段 QApplication 不存在而返回 null,对吧?

互补2

主窗口.cpp

void MainWindow::on_pushButton_clicked()
{
    close();
}

一种解决方案是在 mainwindow.cpp 中添加新的 pushButton 插槽,如上所示。 但是我仍然希望知道如何按照我的方式去做(这篇文章的主要部分)?

补充3:

Alberto 的代码通过使用 QWidget 可以正常工作,如下所示。

ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));

【问题讨论】:

  • 为什么需要在 main.cpp 中如此糟糕地调用它?
  • 那该怎么办呢?我需要退出整个应用程序,但是指向整个应用程序的'a'在main.cpp中,所以我把代码放在里面。
  • 您始终可以通过在代码中的任何位置调用静态 QCoreApplication::instance() 函数来检索指向当前 Q(Core)Application 的指针。
  • 感谢您的回答!请检查我上面的主要帖子,补充部分。
  • 为什么要连接到“关闭”的 SLOT? QCoreApp 没有。您正在寻找的是“退出”SLOT。 (ref)

标签: qt


【解决方案1】:

您的第一种方法行不通,因为正如编译器所说,您没有在 main.cpp 范围内定义 QPushButton。所以编译器找不到它。

正确的方法是您的最后一种方法。您开发一个 .ui 文件,在其中添加一个 QPushButton ,然后在处理您的 .ui 文件(在您的示例中为 QMainWindow 子类)的 c++ 代码中将其与插槽连接。但是您的连接呼叫是错误的。这是我的代码:

//widget.h:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

//widget.cpp:
#include "widget.h"
#include "ui_widget.h"

#include <QtGui>

Widget::~Widget()
{
    delete ui;
}

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
}


//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>93</width>
    <height>41</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


//main.cpp:
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

with: connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close())); 我们可以这样简化:connect(1,2,3,4);

当因为用户在 1 上做了某事而发出 2 时,在 3 上执行 4。

翻译:当用户单击 pushButton(定义到 ui 文件中)时,请关闭此(QMainWindow 子类)。如果您查看您的 main.cpp 代码,MainWindow 是您向用户显示的主要小部件。所以你要关闭整个应用程序。

【讨论】:

  • 我测试了你的代码,它运行良好。但是我的和你的很相似,但它的工作方式不一样!我注意到不同之处在于您使用 QWidget 而我使用 QMainWindow。
  • QMainWindow 和 QWidget 的工作方式相同。所以没关系。也许您添加/删除了其他东西。您可以尝试使用我的代码并添加 QMainWindow 而不是 QWidget。如果您不能解决,请告诉我们。
  • 对不起。我要更新它。我后来又做了一次试验,当时它对 QWidget 和 QMainWindow 都很好。谢谢!
  • 别担心。它必须工作,因为 QMainWindow 继承自 QWidget。我忘了提。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 2013-03-27
  • 2015-06-02
  • 2019-11-26
相关资源
最近更新 更多