[SOLVED] How to initialize ui in the function?



  • In my program, i have created a function. in that function is ui->textEdit->setText(totdMsg[totdNum]); which is crashing my program.
    error: tipoftheday.cpp:22: warning: 'ui' is used uninitialized in this function. Can I have help in this please. How to initialize ui in the function?

    when i replace Ui::TipOfTheDay *ui with Ui::TipOfTheDay *ui = new Ui::TipOfTheDay; then i do not get the warning message but my program still crashes.

    Tipoftheday.cpp
    @#include "tipoftheday.h"
    #include "ui_tipoftheday.h"
    #include "mainwindow.h"
    #include <QDesktopWidget>
    #include <QSettings>
    #include <QCheckBox>

    void DisplayTipOfTheDay();

    TipOfTheDay::TipOfTheDay(QWidget *Child) :
    QDialog(Child, Qt::CustomizeWindowHint |
    Qt::WindowCloseButtonHint ),
    ui(new Ui::TipOfTheDay)
    {
    ui->setupUi(this);
    DisplayTipOfTheDay();

    }

    void DisplayTipOfTheDay(){
    Ui::TipOfTheDay *ui;
    ui->textEdit->setText(totdMsg[totdNum]);
    }

    TipOfTheDay::~TipOfTheDay()
    {
    delete ui;
    }
    @

    tipoftheday.h
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class TipOfTheDay;
    }

    class TipOfTheDay : public QDialog
    {
    Q_OBJECT

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

    private slots:

    private:
    Ui::TipOfTheDay *ui;

    };

    #endif // DIALOG_H@



  • I made some changes to the header file, removing the Ui::TipOfTheDay *ui;, but that only disabled the creation of widgets functions from within the cpp file. I really need some help in this topic.


  • Moderators

    [quote author="kalster" date="1312774533"]
    Tipoftheday.cpp
    @
    void DisplayTipOfTheDay(){
    Ui::TipOfTheDay *ui;
    ui->textEdit->setText(totdMsg[totdNum]);
    }
    @
    [/quote]

    That part does not look healthy at all.
    You create the pointer, but how should the application know where it is pointing?



  • I finally solved it. i created a new function by adding TipOfTheDay:: to the function name that was giving me the error. Now i do not need that pointer in the function. here is my code...

    tipoftheday.cpp
    @void TipOfTheDay::DisplayTipOfTheDay()@

    tipoftheday.h
    @public:
    void DisplayTipOfTheDay();@

    the above two codes work nicely. The program runs fine now.



  • Your problem was in line 21 of tipoftheday.cpp
    You were declaring *ui locally. This local variable overrides the member variable visibility, and hence the error.



  • but i also declared it with the following code and my program still crashed but without the error message.

    @Ui::TipOfTheDay *ui = new Ui::TipOfTheDay@

    is this still declaring it locally. if so then what would be the alternative?



  • You need not declare it locally. You have made it a member variable, remember ?
    (tipoftheday.h line 22).

    You just need to get rid of the line 21 in tipoftheday.cpp and rest should work fine.



  • if i removed line 21 from the cpp then i would get an error message error: 'ui' was not declared in this scope. line 22 in the header was declared as private and therefore i would need to declare it for the function.

    create a new qt gui application and put the following into the mainwindow.cpp file and compile it. you will see the error that i am talking about.

    @void test();
    void test(){
    ui->textEdit->setText("test");
    }@

    anyone know how to get this function to work?


  • Moderators

    [quote author="kalster" date="1312838955"]
    create a new qt gui application and put the following into the mainwindow.cpp file and compile it. you will see the error that i am talking about.

    @void test();
    void test(){
    ui->textEdit->setText("test");
    }@

    anyone know how to get this function to work?[/quote]

    Make it MainWindow::test()

    When you create a function outside the scope of the class (even if it lives in the class's .cpp or .h file), it cannot see the ui variable.



  • yes, in the scope is what i did in my third post. I guess in the scope is the only way. thank you mlong


  • Moderators

    No problem! There aren't really many reasons to have functions outside your class for this sort of thing. So, keeping them in scope is easy in that sense!

    Please be sure and add a [Solved] to the beginning of the title of the thread if you feel like the problem is solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.