Important: Please read the Qt Code of Conduct -

Displaying database values with radio button selection?

  • 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");
        if (!
          QMessageBox::critical(0, QObject::tr("Database Error"),
        QSqlQuery query("SELECT * FROM new_members");
        int index=0;
        while (
            table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
            table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));

    // 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->resize(400, 400);
        QGroupBox *groupBox = new QGroupBox("Radio Buttons");
        QRadioButton *radio1 = new QRadioButton("Radio button 1");
        QVBoxLayout *vbox = new QVBoxLayout;
                                         //MainWindow w;;
        return a.exec(&#41;;


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

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

  • 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 | Joey | 12345 | M | Doe | Doey | 67891 | M

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

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

  • Right solution would be creating your "own model": for representing data and "own delegate": for displaying radiobutton. But if you want use QTableWidget you can set widget for a cell by setIndexWidget().

  • 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?

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

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

  • 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 :-)

  • 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"

  • 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 :)

  • 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 :-).

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

Log in to reply