[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_OBJECTpublic:
explicit TipOfTheDay(QWidget *parent = 0);
~TipOfTheDay();private slots:
private:
Ui::TipOfTheDay *ui;};
#endif // DIALOG_H@
-
[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.
-
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?
-
[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.
-
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.