我想在 Qt GUI 中显示一些带有单选按钮选择的数据库行值。这怎么能实现?我猜这可以使用 foreach 循环来完成。我对以下课程进行了一些研究:
1)QMainWindow 2)QSqlTableModel 3)QTableWidget。
但是哪一个满足我的要求?我无法实现它,请指导我。提前致谢。
我已经在我的源文件中实现了这一点-
主.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();
}