【发布时间】:2015-02-08 21:48:41
【问题描述】:
底线是我有 2 种形式,第一种主窗口第 2 种形式1。我在主窗口上有一个显示表单 2 的按钮。现在我在 form1 上有一个按钮,应该将我带到主窗口,但它不起作用。问题是当我在 form1.h 中说 #include 时,它给了我一个错误,即主窗口没有名称类型。请帮忙,如果可能的话,工作示例会很棒。实际错误是 MainWindow 没有名称类型
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <form1.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui; // i put this line of code in public section when i was trying ui->show(); in form1.cpp file
Form1 obj ; // to show next form
};
#endif // MAINWINDOW_H
mainWindow.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;
}
void MainWindow::on_pushButton_clicked()
{
obj.show();
this->hide();
}
Form1.h
#ifndef FORM1_H
#define FORM1_H
#include <QWidget>
#include<mainwindow.h>
namespace Ui {
class Form1;
}
class Form1 : public QWidget
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0);
~Form1();
private slots:
void on_pushButton_clicked();
private:
Ui::Form1 *ui;
MainWindow mw ; // here i am making object of main window
};
#endif // FORM1_H
form1.cpp
#include "form1.h"
#include "ui_form1.h"
#include<mainwindow.h> // i know when i include this there this issue occurs , but i want to go my previous form to show and for that i have to make its object ! thats how it works when i am going to my next form i.e form1 now i want to go back
Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);
}
Form1::~Form1()
{
delete ui;
}
void Form1::on_pushButton_clicked() // show mainWindow
{
mw->show();
this->hide();
//MainWindow::ui->show(); // i even tried this
}
我也试过如果我可以通过 puttin Ui::MainWindow *ui; 在 form1.h 中不包含 mainwindow.h 来做到这一点在公共部分所以 form1.cpp 文件我可以通过键入 MainWindow::ui->show() 来访问它;这次 ERROR 说 Object is missing Reference to 'MainWindow::ui'
【问题讨论】:
-
mainWindow.h?一些操作系统使用区分大小写的名称(大多数 *nix 名称),因此可能会导致麻烦。另外,发布确切的错误。 -
@zeta,这是错误:'mainWindow' 没有名称类型
-
@Zeta 你可以看到代码。 IDE 自动生成主窗口的表单,因为它是第一个表单。所以我不认为区分大小写是真正的问题。我刚刚尝试了所有 #include
, #include #include 和 #include 没有用 :( ! Zeta 谢谢 :) -
@Zeta 我在 form.h 中尝试过
-
向您的问题添加其他信息,而不是在 cmets 中。确保您的问题易于理解。目前它或多或少是一堵代码墙。尝试将其简化为基本要素(此外,在这一点上,我正在尝试改进您的问题,而不是回答它;那将是答案,而不是评论:D)。
标签: c++ forms qt user-interface