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 :
- QMainWindow
- QSqlTableModel
- 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();
}
@ -
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 | MThank you Mr. Andre, I dont know about QDataWidgetMapper, will study about it now...
-
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().
-
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.
-
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"
-
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.