C++ 如何在Qt C++;
我有两个窗口:主窗口和进度 我无法从主窗口设置progressBar的状态,它位于Progress中 我试着这样做: main window.cpp main window.h 进步.hC++ 如何在Qt C++;,c++,qt,C++,Qt,我有两个窗口:主窗口和进度 我无法从主窗口设置progressBar的状态,它位于Progress中 我试着这样做: main window.cpp main window.h 进步.h #如果没有进展# #定义进度 #包括 命名空间Ui{class Progress;} 课程进度:公共QDialog{ Q_对象 公众: 显式进度(QWidget*parent=nullptr); ~Progress(); 私人: Ui::Progress*Ui_Progress;}; #endif//PROGR
#如果没有进展#
#定义进度
#包括
命名空间Ui{class Progress;}
课程进度:公共QDialog{
Q_对象
公众:
显式进度(QWidget*parent=nullptr);
~Progress();
私人:
Ui::Progress*Ui_Progress;};
#endif//PROGRESS\u H
main.cpp
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[]){
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();}
简而言之,您的progress.h
文件乱七八糟。你添加了不必要的复杂因素,破坏了你的代码
假设您想在Ui
名称空间中定义class Progress
,因为您在那里声明了该类。但是,您在定义之前结束了命名空间。以下是添加了一些注释的代码:
此时,Ui::Progress
的前向声明没有任何用处。你可以摆脱它
namespace Ui {
class Progress : public QDialog { // <-- In the Ui namespace
/* class definition skipped */
}; // end of class Progress
}// end namespace Ui
摘要:声明在命名空间
Ui
中存在具有特定名称的类并不意味着每个具有该名称的类都在命名空间Ui
中。定义也必须在名称空间中,或者通过使用Ui::
前缀将其限定为在名称空间中。我发现的最接近重复的东西是:我更困惑了。。。Ui::Progress*Ui_Progress的定义在mainwindow.h和in Progress.h中。调用Progress.exec()被阻塞。尝试进展。显示()大脑爆炸发生。。。。默认情况下,我的代码版本最初由QtCreator创建。我照你说的做了,但仍然出现了错误:“Ui::Progress”中没有名为“setupUi”的成员。我还注意到方法Progress.show()由于某种原因无法工作。只有progress.exec()可以工作,但它会阻止进一步的执行。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "progress.h"
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void encode_file(); private:
Ui::MainWindow *ui;
Ui::Progress *ui_progress; };
#endif // MAINWINDOW_H
#include "progress.h"
#include "ui_progress.h"
Progress::Progress(QWidget *parent) :
QDialog(parent),
ui_progress(new Ui::Progress) {
ui_progress->setupUi(this); }
Progress::~Progress() {
delete ui_progress; }
#ifndef PROGRESS_H
#define PROGRESS_H
#include <QDialog>
namespace Ui { class Progress; }
class Progress : public QDialog {
Q_OBJECT
public:
explicit Progress(QWidget *parent = nullptr);
~Progress();
private:
Ui::Progress *ui_progress; };
#endif // PROGRESS_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec(); }
namespace Ui { class Progress; } // <-- This line has three parts.
// ^^^^^^^^^^^ ------------------------- This part opens namespace Ui.
// ^^^^^^^^^^^^^^^---------- This part declares that there is a
// class named Progress in that namespace.
// ^-------- This part closes that namespace.
class Progress : public QDialog { // <-- No namespace is open, so this starts a
// definition of a class named Progress in
// the global namespace.
/* class definition skipped */
};
namespace Ui { class Progress; // <-- No closing brace; the namespace remains open.
class Progress : public QDialog { // <-- Now this is in the Ui namespace
/* class definition skipped */
}; // end of class Progress
} // <-- Closes the Ui namespace
namespace Ui {
class Progress : public QDialog { // <-- In the Ui namespace
/* class definition skipped */
}; // end of class Progress
}// end namespace Ui
#include "progress.h"
#include "ui_progress.h"
namespace Ui { // Open the namespace
Progress::Progress(QWidget *parent) :
QDialog(parent),
ui_progress(new Ui::Progress) {
ui_progress->setupUi(this); }
Progress::~Progress() {
delete ui_progress; }
} // Close the namespace