Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Displaying database values with radio button selection?

    General and Desktop
    3
    14
    6840
    Loading More Posts
    • 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.
    • S
      srivatsa last edited by

      Hi, I want to show some database row values with radio button selection in Qt GUI. How this can be accomplished?. This could be done using foreach loop I guess. I have studied a bit about the following classes :

      1. QMainWindow
      2. QSqlTableModel
      3. QTableWidget.

      But which one satisfies my requirement? I am not able to implement it, please guide me. Thanks in advance.

      I have implemented upto this in my sourcefile main.cpp:

      @#include <QtGui/QApplication>
      #include <QtSql>
      #include <QTableWidget>
      #include <QMessageBox>
      #include "mainwindow.h"
      #include <QRadioButton>
      #include <QVBoxLayout>
      #include <QGroupBox>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);

      QTableWidget* table = new QTableWidget();
      table->setWindowTitle("Connect to Mysql Database Example");
      
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
          db.setHostName("localhost");
          db.setDatabaseName("guests");
          db.setUserName("sri");
          db.setPassword("******");
          if (!db.open())
          {
            QMessageBox::critical(0, QObject::tr("Database Error"),
            db.lastError().text());
          }
      
          QSqlQuery query("SELECT * FROM new_members");
      
          table->setColumnCount(query.record().count());
          table->setRowCount(query.size());
      
          int index=0;
          while (query.next())
          {
      
              table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
              table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
      
              index++;
      
          }
      

      // This is sample radiobutton from QGroupBox class. Like this I need to implement the values from DB in with radio button selections for each value

          QMainWindow *window = new QMainWindow();
          window->setWindowTitle(QString::fromUtf8("QGroupBox"));
          window->resize(400, 400);
          QGroupBox *groupBox = new QGroupBox("Radio Buttons");
          QRadioButton *radio1 = new QRadioButton("Radio button 1");
          radio1->setChecked(true);
          QVBoxLayout *vbox = new QVBoxLayout;
          vbox->addWidget(radio1);
          groupBox->setLayout(vbox);
          window->setCentralWidget(groupBox);
          window->show();
      
          table->show();
                                           //MainWindow w; w.show();
      
          return a.exec(&#41;;
      

      }
      @

      1 Reply Last reply Reply Quote 0
      • Q
        qxoz last edited by

        What sellection you need by radiobutton?
        Between rows of table, one of separate values for each row or something else?

        1 Reply Last reply Reply Quote 0
        • A
          andre last edited by

          Did you take a look at [[doc:QDataWidgetMapper]]?

          1 Reply Last reply Reply Quote 0
          • S
            srivatsa last edited by

            Thanks a lot qxoz for your reply, I want to show Email_Id values recursively in that window with radio button besides it, when Administrator selects one and clicks on Next> he would get all information of that particular Email_Id in the next window containing respective form fields.

            Here is the sample Table:

            Email_Id | First Name | Last Name | Contact Number | Gender
            joe@doe.com | Joe | Joey | 12345 | M
            doe@joe.com | Doe | Doey | 67891 | M

            Thank you Mr. Andre, I dont know about QDataWidgetMapper, will study about it now...

            1 Reply Last reply Reply Quote 0
            • S
              srivatsa last edited by

              Mr.QXOZ, Mr. Andre, can you explain me more clearly please... Thank you in advance :)

              1 Reply Last reply Reply Quote 0
              • Q
                qxoz last edited by

                Right solution would be creating your "own model":http://doc.qt.digia.com/4.7-snapshot/modelview.html for representing data and "own delegate":http://doc.qt.digia.com/stable/itemviews-spinboxdelegate.html for displaying radiobutton. But if you want use QTableWidget you can set widget for a cell by setIndexWidget().

                1 Reply Last reply Reply Quote 0
                • A
                  andre last edited by

                  I am not really sure what it is you want to achieve exactly. Is all you want to do display a table from a database, and then showing a radio button before each row?

                  1 Reply Last reply Reply Quote 0
                  • S
                    srivatsa last edited by

                    Yeah Mr.Andre, that's perfectly guessed though not a table whole together but just the primary key values with radio buttons for each of the value (its Email_Id in my case), so when Admin selects this one radio button value, and clicks on submit button, the next window must be populated with form fields displaying information of that particular Email_Id.

                    1 Reply Last reply Reply Quote 0
                    • Q
                      qxoz last edited by

                      Do you realy need radiobutton list? What if use just ListView with single selection? I mean without any radio buttons in the list.

                      1 Reply Last reply Reply Quote 0
                      • A
                        andre last edited by

                        Sure, single selection on a normal tree view would be the obvious choice. If you really want, you can however visually represent the selection with a radio button using a proxy model. The proxy model could take the QItemSelectionModel. Depending on if the row is selected, it could give a different result for the Qt::DecorationRole of the first column. Just use two images, one of a checked and one of an unchecked radio button. For bonus points, use the current style to render them :-)

                        1 Reply Last reply Reply Quote 0
                        • S
                          srivatsa last edited by

                          Well good question Mr.QXOZ, I will explain the remaining application perspective now:

                          "Radio button selection is a must, the DB Table contains extra information like what type of gifts they need, because after viewing the information of that particular Email_Id, I would like to create a particular gift box for each and every user based on this information. Gift box just includes some documents and some secrets ;-). And all this packing is included in my Qt App"

                          1 Reply Last reply Reply Quote 0
                          • Q
                            qxoz last edited by

                            And still, RadioButtons are just a visual effect, selected item you can get from any list. Ofcourse if design needs a such look then Andre's solution is easy and lite for implement. Or i still don't understand your goals :)

                            1 Reply Last reply Reply Quote 0
                            • S
                              srivatsa last edited by

                              Well Mr.Andre and Mr.QXOZ, as I am new to Qt, First I will study the Classes Information that you have mentioned. After implementing or trying, I will revert back to you, Thanks again :-).

                              1 Reply Last reply Reply Quote 0
                              • A
                                andre last edited by

                                I'd advise you to leave the whole idea of using radio buttons for the selection alone for now, and just use a table configured to use single row selection. You can always add the visual effect of a radio button later. First get the rest of your application to work before worrying about details like this.

                                Note that using radio buttons in item views is very non-standard. Checkboxes are used frequently, but radio buttons will look quite alien.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post