0

I have a problem in Qt. I want to use "ui" in another class function.

With this code:

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

I get these errors:

  1. error C2227: left of '->qTextBrowser' must point to class/struct/union
  2. error C2227: left of '->append' must point to class/struct/union

And with this code:

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

I get this error: error C2227: left of '->append' must point to 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:

};

What can I do?

ps:Excuse my bad English, i'm French

3
  • 3
    You have to point to a variable, and not to the class names. What is Ui::MainWindow and how it looks like? Commented Oct 15, 2015 at 10:09
  • 2
    -> must point to an object of the class and it seems that you are using directly the class. Commented Oct 15, 2015 at 10:09
  • If you're getting strange crashes, paste the code of your main function as well. Commented Oct 15, 2015 at 18:21

2 Answers 2

1

If you are referring to default project created by Qt, ui can't be used as it is private. Make a MainWindow object and use it (like it is used in main()).

Now, if you have a QTextBrowser object created in MainWindow, call using that object and not class signature as:

ui->objTextBrowser->append("Test")
Sign up to request clarification or add additional context in comments.

2 Comments

ty but if i make a new MainWindow the program crash and i get this message error: QWidget:Must construct a QApplication before a QWidget
Yes because QApplication is required for UI. Qt by default generates this code in main(): QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); Try with this.
0
  1. If "test" is class or struct it has to know about MainWindow object or in particular about it's child object TextBrowser.

  2. Ui creates in the MainWindow constructor so, before using it you have to create it.

And in addition it's a bad practice to do what you want to do, the better solution is to connect signals from your test class (that have to be inherit from QObject) to slot of MainWindow

So bad practice looks:

    //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()
{

}

Read about signals and slots to get own solution of what you want using signals and slots. And of course read more about theory of C++ to understand what is private members of classes and structures, what is namespaces and scoping

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.