c++ QT在另一个类中使用ui错误

C++ QT error using ui in another class

本文关键字:ui 错误 QT 另一个 c++      更新时间:2023-10-16

我在Qt中有一个问题,我想在另一个类函数中使用"ui"。

使用下面的代码:

void test::TextAp()
{
MainWindow::ui->QTextBrowser->append("Test");
}

我得到这些错误:

  1. 错误C2227: '->qTextBrowser'的左边必须指向class/struct/union
  2. 错误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")
  1. 如果"test"是类或结构,它必须知道MainWindow对象,特别是它的子对象TextBrowser。

  2. 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++的理论来理解什么是类和结构的私有成员,什么是命名空间和作用域