2015-10-15 20 views
0

我在Qt中遇到问题。我想在另一个类的函数中使用“ui”。在另一个类中使用ui的C++ QT错误

有了这个代码:

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

我得到这些错误:

  1. 错误C2227:左 ' - > qTextBrowser' 必须指向类/结构/联合
  2. 错误C2227: ' - > append'必须指向类/结构体/联合体

并使用此代码:

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

我得到这个错误:错误 C2227:左 ' - >追加' 必须指向类/结构/联合

MainWindow.h:

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

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

private: 

}; 

我能做些什么?

PS:对不起,我的英语不好,我是法国人

+3

你必须指向一个变量,而不是类名。什么是'Ui :: MainWindow'和它的外观? – vahancho

+2

- >必须指向该类的一个对象,并且似乎您正在直接使用该类。 – acostela

+0

如果遇到奇怪的崩溃,请粘贴main函数的代码。 – RobbieE

回答

1

如果你指的是使用Qt创建默认项目,ui不能使用,因为它是私人的。制作一个MainWindow对象并使用它(就像它在main()中使用的一样)。现在

,如果你有使用对象,而不是类签名在主窗口中创建一个QTextBrowser对象,通话时间为:

ui->objTextBrowser->append("Test") 
+0

ty但如果我使新的MainWindow程序崩溃,并且我收到此消息错误:QWidget:必须在QWidget之前构造一个QApplication – Felusogar

+0

是的,因为QApplication是UI所必需的。 Qt默认在main()中产生这个代码:'QApplication a(argc,argv); MainWindow w; w.show(); 返回a.exec();'尝试这个。 – Aman

+0

应用程序崩溃没有消息错误 – Felusogar

0
  1. 如果“测试”是类或结构有了解主窗口对象或特别是关于它的子对象TextBrowser。

  2. Ui在MainWindow构造函数中创建,所以在使用它之前必须创建它。

而且除了这是一个不好的做法,做你想做的事,更好的解决方案是连接从测试类的主窗口

时隙信号(即必须继承自QObject)如此糟糕的做法看起来:

//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++的理论知道什么是类和结构的私有成员,命名空间和范围是什么