Unsolved Advice about the application I will be making? (I'm a beginner)
-
@mrjj
I don't know. But I want to define a global object so that other functions can use it. But I just don't know where to declare it -
@dvlpr.bernard
Hi
Just like the
QString ch_topic;
Then all member functions have access to it as they are part of the class. -
@mrjj
whenever I use qDebug() <<get_ch_Topic.getTopic() in the same function where ch_topic was define it returns "IF". But when I use qDebug() <<get_ch_Topic.getTopic() in other functions it return "". I don't know why but maybe because the object name was different?
-
Hi
Do NOT make new instances of the main window class like you do
that is just plain wrong.
You are already in an instance. So no need to ever make new ones.
you can simply use the ch_topic variable.
Its already declared in the MainWindow you are using already. -
@mrjj
Sir, that's what I did before but whenever I do that it says -
well
just use the variable directly. no need to use function.
im not sure what get_ch_tipic is as you dont have () so its not function call.However, if you do
QString MyX; ( in .h as you shown)
then anywhere you can just use it like
MyX = "";
or read it.
is just bad syntax.
as you say get_ch_Topic is a object with the function getTopic and its not true.if you gave MainWindow a function called getTopic
then just call it
qDebug() << getTopic();Its important to understand that all you add to MainWindow class ( in .h)
can be access from any functio that also a memebr .
that is
MainWindow::SomeFunction() {
any variable from .h you can use here. no special syntax. just use it.
} -
Thank you sir. I just don't know but still it doesn't work it just return "" not "WHILE". :(
-
@dvlpr.bernard
hi
check code where you set it.
Maybe you create other instance you set it on ? -
@mrjj
Hello sir,
Where should I declare a global instance / object in my 3 files.
mainwindow.h, main.cpp, or mainwindow.cpp?
Ang how whenever I declare one because whenever I declare one e.g. (MainWindow get_ch_Topic;) it display an error "no previous extern declaration for non-static variable" -
@dvlpr.bernard said in Advice about the application I will be making? (I'm a beginner):
Where should I declare a global instance / object in my 3 files.
Nowhere! Avoid global variables especially if those are derived from QObject!
Why do you need global variables at all? -
This post is deleted! -
@jsulm
Hello sir. So that I can access the string that I declared in other functions.
How could I do it the right way?0_1556355591803_f0274585-e5f9-4e11-8428-9341f8853a75-image.png
-
Hi
Put all the variables in the class!class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
...
private:
Ui::MainWindow *ui;
QString ch_Dif_Lvl; ///// HERE !
QString fc_Topic;
};then you can just use them
void MainWindow::on_pushButton_clicked() { ch_Dif_Lvl = "BEGINNER"; fc_Topic = "Topic"; }
-
@mrjj
Thank you. God bless your kindness to help other people. -
@dvlpr.bernard
Thank you :)
I hope the project is progressing fine. -
@mrjj
Good day sir.I have been trying to get the data from my database it is already connected but whenever the query executes qDebug() returns "QSqlQuery::exec: database not open".
Also I tried to replace this query
qry.exec("select Question from mydatabase where QQuantity = 4 and Topic = 'FOR' and Difficulty_Level = 'ADVANCED'")
with variable and string name
qry.exec("select Question from mydatabase where QQuantity = '"+quantity+"'and Topic = '"ch_topic"' and Difficulty_Level = '"ch_dif_lvl"'")
but it didn't work also. -
Hi
I cant see anything wrong.
Show how code is before the if ( mydb.open ) -
The code above is inside of this button function
Here's the declaration of the database
-
-
I notice that your Output window shows two occurrences of your message
Connected...
. Why is that? Are you connecting more than once?? [In a separate issue, in the long run you absolutely will not want toopen()
and thenclose()
the database for each query. The whole point is to connect once, keep the connection open while you execute all your queries, and close when you are completely done with the database.] -
[...] but it didn't work also.
What does that mean? Error message? Behaviour? What?? Please help us to help you by supplying useful information.
-
-