c++ QT在另一个类中使用ui错误
C++ QT error using ui in another class
我在Qt中有一个问题,我想在另一个类函数中使用"ui"。
使用下面的代码:
void test::TextAp()
{
MainWindow::ui->QTextBrowser->append("Test");
}
我得到这些错误:
- 错误C2227: '->qTextBrowser'的左边必须指向class/struct/union
- 错误C2227: '->append'的左边必须指向class/struct/union
加上下面的代码:
void test::TextAp()
{
Ui::MainWindow::QTextBrowser->append("Test");
}
我得到这个错误:'->append'的左边必须指向class/struct/union
MainWindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
private:
};
我能做什么?
请原谅我英语不好,我是法国人如果你指的是Qt创建的默认项目,ui
不能使用,因为它是私有的。创建一个MainWindow对象并使用它(就像在main()中使用它一样)。
现在,如果你在MainWindow中创建了一个QTextBrowser对象,使用该对象而不是类签名调用:
ui->objTextBrowser->append("Test")
-
如果"test"是类或结构,它必须知道MainWindow对象,特别是它的子对象TextBrowser。
-
Ui在MainWindow构造函数中创建,所以在使用它之前你必须先创建它。
此外,做你想做的事情是一个不好的做法,更好的解决方案是将你的测试类(必须从QObject继承)的信号连接到MainWindow的插槽
So bad practice look:
//main.cpp
#include "test.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//instance of MainWindow
MainWindow w; // here constructor of MainWindow creates ui and all the childs
w.show();
//instance of test struct
test t;
//behind the design mode is creation of code with objects that you can create "manually" typing them
//to see the ui additional files just press Ctrl and mouse click (for example) on function ui->setupUi(this) in MainWindow constructor
//it's automatically generated code for ui created according to what you've created in design mode
//so here textBrowser pointer of object t is points to textBroswer of ui of object w
t.textBrowserFromTestStruct = w.findChild<QTextBrowser*>("textBrowser");
//get error if object f ui has no QTextBrowser named textBrowser
Q_ASSERT(t.textBrowserFromTestStruct);
//invoke t object function to append text to f textBrowser 10 times
for(int i = 0; i < 10; ++i)
t.TextAp("Hello World ");
return a.exec();
}
//test.h
#ifndef TEST_H
#define TEST_H
#include "mainwindow.h"
#include <QTextBrowser>
struct test
{
QTextBrowser *textBrowserFromTestStruct;
public:
void TextAp(QString text){textBrowserFromTestStruct->append(text);}
};
#endif // TEST_H
//mainwindow.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();
Ui::MainWindow *getUI(){return ui;}
private:
Ui::MainWindow *ui;
};
#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()
{
}
阅读信号和插槽,以获得您想要使用信号和插槽的自己的解决方案。当然还要多读一些c++的理论来理解什么是类和结构的私有成员,什么是命名空间和作用域
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- wxWidgets UI应用程序在C++中使用4个或多个wxBoxSizer对象时出现分段错误
- 我运行一个程序并遇到分段错误(核心转储),我使用 gdb 进行调试,程序在运行 ui->setupUI() 时崩溃了
- c++ QT在另一个类中使用ui错误
- 使用 C++11 编译 UI 代码时面临"unable to find string literal operator"错误