Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I'd like to put only the name of the database column in the QcomboBox and link it with the chart.
Forum Updated to NodeBB v4.3 + New Features

I'd like to put only the name of the database column in the QcomboBox and link it with the chart.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 461 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meria0503
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • M meria0503

      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?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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 using QSqlTableModel or QSqlQueryModel you can get that list via QSqlRecord, 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));
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        meria0503
        wrote on last edited by
        #3

        It's a little hard to understand. Do you happen to know the full code?

        JonBJ 1 Reply Last reply
        0
        • M meria0503

          It's a little hard to understand. Do you happen to know the full code?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            meria0503
            wrote on last edited by
            #5

            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.

            JonBJ 1 Reply Last reply
            0
            • M meria0503

              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.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @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 named COLS 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?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                meria0503
                wrote on last edited by
                #7

                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?

                JonBJ 1 Reply Last reply
                0
                • M meria0503

                  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?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @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"?

                  M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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"?

                    M Offline
                    M Offline
                    meria0503
                    wrote on last edited by meria0503
                    #9

                    @JonB

                    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?

                    JonBJ 1 Reply Last reply
                    0
                    • M meria0503

                      @JonB

                      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?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @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.

                      M 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @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.

                        M Offline
                        M Offline
                        meria0503
                        wrote on last edited by
                        #11

                        @JonB

                        I found the cause and solved it. I'm sorry to bother @JonB with my lack of knowledge. I'm sorry.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved