两个combobox相连问题
写了一段小代码,目的是用两个combobox显示一个城市数据库的内容,第一个combobox显示省,第二个显示城市,现在的问题是如何在第一个显示省改变的时候,比如北京切换到河北省的时候,第二个能顺利的从北京切换到河北省的相关城市,如下是我的代码:
citywindow.h
#include <QtGui>#include "ui_citywindow.h"class CityWindow : public QWidget , public Ui::cityWindow{ Q_OBJECTpublic: CityWindow(QWidget *parent=0); bool createConnection(); void initcitylist(); void currentCityChanged(int num);protected:private slots: void closeWindow();private: QSqlDatabase db;};#endif // CITYWINDOW_H
#include "citywindow.h"CityWindow::CityWindow(QWidget *parent) :QWidget(parent){ setupUi(this); connect(exitButton,SIGNAL(clicked()),this,SLOT(closeWindow())); db=QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("city.db"); initcitylist(); //connect(provinceBox,SIGNAL(currentIndexChanged(int)),cityBox,SLOT(setCurrentIndex(int)));}void CityWindow::closeWindow(){ close();}void CityWindow::initcitylist(){ bool ok=db.open(); provinceBox->clear(); cityBox->clear(); if(ok) { QSqlQuery query; query.exec("SELECT sName,sCode FROM tCity"); while(query.next()) { if(query.value(1).toInt() < 35 ) { provinceBox->insertItem(query.value(1).toInt() -1, query.value(0).toString()); } else break; } } db.close(); currentCityChanged(0);}void CityWindow::currentCityChanged(int num){ qDebug("222"); bool ok = db.open(); cityBox->clear(); if(ok) { QSqlQuery query; QString execStr = QString("SELECT sName FROM tCity WHERE sBelongCode=%1").arg(num+1); qDebug("333"); query.exec(execStr); while(query.next()) { qDebug("444"); cityBox->addItem(query.value(0).toString()); qDebug("555"); } } db.close();}
#include <QApplication>#include <QtGui>#include <QtSql>#include "citywindow.h"int main(int argc,char *argv[]){ QApplication app(argc,argv); CityWindow window; window.show(); return app.exec();}