Jump to content

so this is probably a easy thing i guess but i haven't managed to figure this out myself or managed to find a explanation for this online. i'm very new to C++. im using QT and i have a label in my GUI, i want to display some text in it. this is what i have tried but i keep getting ui was not declared in the scope.

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{

  ui->label->setText("hello");

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

i have also tried this in the main method but no luck.  

Ui::MainWindow->label->setText("hello");

error i got for this was: expected unqualified-id before '->' token Ui::MainWindow->label->setText("hello"); 

Link to comment
https://linustechtips.com/topic/856390-how-to-use-a-label-from-maincpp/
Share on other sites

Link to post
Share on other sites

4 hours ago, Shammikit said:

so this is probably a easy thing i guess but i haven't managed to figure this out myself or managed to find a explanation for this online. i'm very new to C++. im using QT and i have a label in my GUI, i want to display some text in it. this is what i have tried but i keep getting ui was not declared in the scope.


#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{

  ui->label->setText("hello");

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

i have also tried this in the main method but no luck.  


Ui::MainWindow->label->setText("hello");

error i got for this was: expected unqualified-id before '->' token Ui::MainWindow->label->setText("hello"); 

There is no reason to do this, main should not be touched. All it does is create the 'QApplication' and 'MainWindow', shows the 'MainWindow' and runs the application. If you want to change the label's text when the 'MainWindow' is created (on program start), add your 'setText' line in the 'MainWindow's constructor, after the 'setupUi' line:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->label->setText("Hello, World!");
}

 

If the label text should change when a event happens, such as a button press, add the line to the event handler (slot):

void MainWindow::on_pushButton_clicked()
{
    ui->label->setText("Hello, World!");
}

 

'ui' is a pointer to a 'Ui::MainWindow' object, where 'Ui' is a namespace. Take note that this makes it differ from the 'MainWindow' class itself (outside the 'Ui' namespace). The pointer is a private member in 'MainWindow', which is why it is not accessible outside 'MainWindow'. The 'Ui::MainWindow' class is auto-generated each time you compile in a file called 'ui_mainwindow.h', incorporating your graphical window design. Check the build-output folder to find it if you're curious.

 

A new instance of this 'Ui::MainWindow' is then created, assigned to the 'ui' pointer, and initialized (by calling it's setupUi' member) by 'MainWindow's constructor. It is deleted by 'MainWindow's destructor. Thus it only exists and is valid during the lifetime of 'MainWindow'.

 

If you really, really wanted to you could do it. By adding a public memberfunction to 'MainWindow' that returns the 'ui' pointer:

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    Ui::MainWindow* GetUi() { return ui; }    //<<=======This line added!

private slots:
    void on_pushButton_clicked();
    
private:
    Ui::MainWindow *ui;
};

You could then access it from main, but only after the 'MainWindow' has been created:

#include "mainwindow.h"
#include <QApplication>

#include "ui_mainwindow.h"	//<===== Added to get access to Ui::MainWindow definition.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    w.GetUi()->label->setText("The quick brown fox...");  //<== Added.

    w.show();
    return a.exec();
}

But again, there's no reason to and would be bad style. Only a window should manage it's own controls, handing out pointers to everyone will quickly result in spaghetti-code where everything messes with everything. 
 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×