I'd like to put only the name of the database column in the QcomboBox and link it with the chart.
-
I'm not using QcomboBox well yet. I'm going to put only the DB column name in the QcomboBox and express the chart through the sql statement that contains the name. Here's the order
Select a calendar - Select a date when you click on the calendar - Select from its DB column QcomboBox - Chart representation
I want to make it this way.
Is there a way to put a database column in QcomboBox?
-
I'm not using QcomboBox well yet. I'm going to put only the DB column name in the QcomboBox and express the chart through the sql statement that contains the name. Here's the order
Select a calendar - Select a date when you click on the calendar - Select from its DB column QcomboBox - Chart representation
I want to make it this way.
Is there a way to put a database column in QcomboBox?
@meria0503
Of course there is! Use QComboBox::setModel(QAbstractItemModel *model) to bind the combobox to your database model and setModelColumn(int visibleColumn) to the desired column number in the model. The combobox will display all of that column's possible values found in the model.Now, that would give you a combobox with all the values in one column in the database. I wonder if what you are wanting is actually to have a list of the columns in the database/model, so that the user can choose one whose row values will then be used for the chart? In that case you need a list of those column names, then you can put them in the combobox. How you get that depends on what database/model you are using. Are you using the Qt
QSql...
database accessors? If you are usingQSqlTableModel
orQSqlQueryModel
you can get that list viaQSqlRecord
, e.g.:QSqlTableModel table = QSqlTableMode(); table.setTable("table_name"); QSqlRecord record = table.record(); for (int col = 0; col < record.count(); col++) combobox.addItem(record.fieldName(col));
-
@meria0503
Me? Full code for what? You have not even commented on what you want. If you want a list of the names of the columns from a table in a SQL database I have given you the code. -
What I want is to put the column in my DB in QComboBox as mentioned above.
QSqlQueryModel *model = new QSqlQueryModel; QString sql; sql = "SELECT COLUMN_NAME FROM COLS WHERE TABLE_NAME = 'HMIDATA'"; QSqlQuery query; if(!query.exec(sql)){ qDebug () <<"query error:" << query.lastError(); } else { qDebug () <<"query success:" << query.lastQuery(); } model->setQuery(query); ui->comboBox_Select_2->setModel(model); ui->comboBox_Select_2->show();
The current code is like this, but the Qcombobox contains a column, and if you select it, the program will be forced to exit.
-
What I want is to put the column in my DB in QComboBox as mentioned above.
QSqlQueryModel *model = new QSqlQueryModel; QString sql; sql = "SELECT COLUMN_NAME FROM COLS WHERE TABLE_NAME = 'HMIDATA'"; QSqlQuery query; if(!query.exec(sql)){ qDebug () <<"query error:" << query.lastError(); } else { qDebug () <<"query success:" << query.lastQuery(); } model->setQuery(query); ui->comboBox_Select_2->setModel(model); ui->comboBox_Select_2->show();
The current code is like this, but the Qcombobox contains a column, and if you select it, the program will be forced to exit.
@meria0503
So you did want the available column names in a table to be placed in a combobox. I showed you how to do it using Qt calls independent of the database driver. Your way relies on some table namedCOLS
in some particular SQL implementation. That's OK if it works for you.and if you select it, the program will be forced to exit.
This does not mean anything to me/anybody. Presumably you have verified that after your code the combobox is populated with the (correct) names of the columns, right? So the SQL query phase is done and has worked. You now have a combobox with some strings in it. Selecting a string in itself will do nothing, and won't exit the program. Maybe you have some other code attached to the combo value change? Maybe that code has some problem in it?
-
All the columns are normally in QComboBox. However, there is a code that includes sql that loads the chart in some of my codes, but if you don't load it first, if you select QComboBox first, the program will end immediately. Conversely, if you load the chart first and then select QComboBox, it is loading normally. Is the query the problem?
-
All the columns are normally in QComboBox. However, there is a code that includes sql that loads the chart in some of my codes, but if you don't load it first, if you select QComboBox first, the program will end immediately. Conversely, if you load the chart first and then select QComboBox, it is loading normally. Is the query the problem?
@meria0503 said in I'd like to put only the name of the database column in the QcomboBox and link it with the chart.:
Is the query the problem?
- If the result of your code is that the combobox is filled correctly with the names of the columns then the query has worked.
- If the correct column names do not appear in the combobox then the query is wrong.
You can eliminate this query from being the issue. Let's say it currently returns 3 columns, named
abc
,def
&ghi
. Then temporarily get rid of this SQL query and simply go:ui->comboBox_Select_2->addItems( { "abc", "def", "ghi" } );
Now you should be in the same situation, but without that SQL query to worry about. Does this result in any change in behaviour?
Assuming this behaves the same as before, your problem must lie in code which uses the selected item. Which you do not show.
Have you run this program via Debug from Creator? Do you get any information about how/why/where it "will end immediately"?
-
@meria0503 said in I'd like to put only the name of the database column in the QcomboBox and link it with the chart.:
Is the query the problem?
- If the result of your code is that the combobox is filled correctly with the names of the columns then the query has worked.
- If the correct column names do not appear in the combobox then the query is wrong.
You can eliminate this query from being the issue. Let's say it currently returns 3 columns, named
abc
,def
&ghi
. Then temporarily get rid of this SQL query and simply go:ui->comboBox_Select_2->addItems( { "abc", "def", "ghi" } );
Now you should be in the same situation, but without that SQL query to worry about. Does this result in any change in behaviour?
Assuming this behaves the same as before, your problem must lie in code which uses the selected item. Which you do not show.
Have you run this program via Debug from Creator? Do you get any information about how/why/where it "will end immediately"?
I think there's something wrong with my code
ui->comboBox_Select_2>addItems({"abc", "def", "ghi"}); << this was added
17:14:17: The program has unexpectedly finished.
17:14:17: The process was ended forcefully.
17:14:17: C:/QTQT/build-Infomanager-Desktop_Qt_5_12_4_MSVC2017_64bit-Debug/debug/Infomanager.exe crashed.The message was displayed and forced to exit.
As you said, I ran it with Debug from Creator.
There is a mouse event in the code content, can this be the cause?
-
I think there's something wrong with my code
ui->comboBox_Select_2>addItems({"abc", "def", "ghi"}); << this was added
17:14:17: The program has unexpectedly finished.
17:14:17: The process was ended forcefully.
17:14:17: C:/QTQT/build-Infomanager-Desktop_Qt_5_12_4_MSVC2017_64bit-Debug/debug/Infomanager.exe crashed.The message was displayed and forced to exit.
As you said, I ran it with Debug from Creator.
There is a mouse event in the code content, can this be the cause?
@meria0503 said in I'd like to put only the name of the database column in the QcomboBox and link it with the chart.:
As you said, I ran it with Debug from Creator.
Then I would have expected Creator debugger to stop when
The program has unexpectedly finished.
and show you the location of the problem and a stack trace back to your code for when it happened.If you do not/cannot get this, for some unknown reason, then sprinkle
qDebug()
statements throughout your code to see what/where the last one output got to.You have added:
There is a mouse event in the code content, can this be the cause?
What does "code content" mean? If you have a proper stack trace when it "finishes unexpectedly" then show that. I cannot play a guessing game from your remarks.
-
@meria0503 said in I'd like to put only the name of the database column in the QcomboBox and link it with the chart.:
As you said, I ran it with Debug from Creator.
Then I would have expected Creator debugger to stop when
The program has unexpectedly finished.
and show you the location of the problem and a stack trace back to your code for when it happened.If you do not/cannot get this, for some unknown reason, then sprinkle
qDebug()
statements throughout your code to see what/where the last one output got to.You have added:
There is a mouse event in the code content, can this be the cause?
What does "code content" mean? If you have a proper stack trace when it "finishes unexpectedly" then show that. I cannot play a guessing game from your remarks.