[SOLVED] How to initialize ui in the function?
-
[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.