Advice about the application I will be making? (I'm a beginner)
-
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". :(
-
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 ? -
@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" -
@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? -
@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! -
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"; }
-
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. -
@mrjj
Thank you. God bless your kindness to help other people.@dvlpr.bernard
Thank you :)
I hope the project is progressing fine. -
@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
-
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.
-
-
-
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.
-
-
I played around with the code and this error return.
-
-
hi
are you sure the table name is mydatabase ?
seems wrong. unless the table is called taht.
But it seems you used the name of the database.and it even says unknown table.
so use table name instead. -
no such table: mydatabase
@mrjj wrote:
are you sure the table name is mydatabase ?
Are you getting your database vs your tables mixed up in your head? You work with one database at a time. In SQLite that corresponds to the file you pass to
setDatabaseName()
, and is what gets opened viamydb.open()
. A database may contain many tables, each with their own name. It is the table name you mention in SQLselect
(and most other) statements. You previously created your table(s) via some kind ofCREATE TABLE name ...
statement, and that name is what you need in yourquery
. It would be "unusual" for you to have named a tablemydatabase
.You might like to do yourself a favour, to help with all the SQLite queries you're trying to write. Download one of the (free) "workbenches" for SQLite. These are GUI applications which will let you see what is in your database; it will also let you construct and play with queries, till you are ready to copy them over to your Qt code. Google for
sqlite workbench
, there seem to be several, e.g. https://www.sqlite-workbench.com/, https://sqlitestudio.pl, https://alternativeto.net/software/sqlite-manager/. I suggest you take the time to pick one, in the long run I think the effort will be well worthwhile for you.