DynamicCellModelLoaderPtr DynamicCellModelLoader::Create(const std::string& rLoadableModulePath) { DynamicCellModelLoaderPtr p_loader(new DynamicCellModelLoader(rLoadableModulePath)); return p_loader; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_sqlModel(0), m_workerThread(new QThread), m_proxy(0) { QCoreApplication::addLibraryPath("./sqldrivers"); QPluginLoader p_loader(QCoreApplication::applicationDirPath() + "/sqldrivers/qsqlmysql4.dll"); p_loader.load(); qDebug() << p_loader.isLoaded(); qDebug() << "Starting new window " << QThread::currentThread(); tableView = new QTableView(this); tableView->horizontalHeader()->setMovable(true); QString newMenuTitle = QString::fromUtf8("编辑"); QMenu* new_menu = QMainWindow::menuBar()->addMenu(newMenuTitle); QAction* new_result_act = new_menu->addAction(QString::fromUtf8("新建开奖结果")); QAction* delete_result_act = new_menu->addAction(QString::fromUtf8("删除结果")); // add a new separator new_menu->addSeparator(); // the action use to trigger the data import functin QAction* import_data_act = new_menu->addAction(QString::fromUtf8("导入开奖结果")); connect(new_result_act, SIGNAL(triggered(bool)), SLOT(onNewActionClick())); connect(delete_result_act, SIGNAL(triggered(bool)), SLOT(onDeleteActionClicked())); connect(import_data_act, SIGNAL(triggered(bool)), SLOT(onImportDataClicked())); setCentralWidget(tableView); LotteryDockWidget *myDockWidget = new LotteryDockWidget(this); QWidget *title_bar = new QWidget(this); // trick to remove the title bar of dock widget myDockWidget->setTitleBarWidget(title_bar); myDockWidget->setAllowedAreas(Qt::TopDockWidgetArea); addDockWidget(Qt::TopDockWidgetArea, myDockWidget); // hash table for setting strings QHash<QString, QString> config_hash; // open the configuration file QFile config_file(QCoreApplication::applicationDirPath() + "/config.txt"); config_file.open(QIODevice::ReadOnly); memset(buffer, '\0', 64); int readLength = config_file.readLine(buffer, 64); while (readLength != -1) { QString line(buffer); QStringList list1 = line.split(" = "); // remove carriage return list1[1].remove(0x0D); // remove line feed list1[1].remove(0x0A); config_hash.insert(list1[0], list1[1]); memset(buffer, '\0', 64); readLength = config_file.readLine(buffer, 64); } config_file.close(); db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(config_hash.value("host_name")); db.setUserName(config_hash.value("user")); db.setPort(3306); db.setDatabaseName(config_hash.value("database")); db.setPassword(config_hash.value("password")); bool open_ok = db.open(); if (!open_ok) { QErrorMessage err_msg; QString error_str = QString::fromUtf8("开启数据库失败.请重启程序") + "\n" + db.lastError().text() + "\n" + QString::number(db.lastError().type()) + "\n" + db.hostName() + db.password() + db.userName() + db.databaseName(); if (!p_loader.isLoaded()) error_str += "\Plugin not loaded"; err_msg.showMessage(error_str); err_msg.exec(); exit(-1); } m_sqlModel = new MyModel(0, db); connect(myDockWidget, SIGNAL(blueBallSelectChanged(int)), m_sqlModel, SLOT(setBlueBallSelect(int))); connect(myDockWidget, SIGNAL(redBlueSeparateChanged(bool)), m_sqlModel, SLOT(setRedBlueSeparated(bool))); // use the current selection from the dock widget to dertermine how the model selects data m_sqlModel->setRedBlueSeparated(myDockWidget->isRedBlueSeparated()); // call this function to make sure that the right table is used // use previsouly set boolean value m_sqlModel->setRedBlueSeparated(m_sqlModel->isRedBlueSeparated()); tableView->setModel(m_sqlModel); tableView->verticalHeader()->hide(); m_sqlModel->setEditStrategy(QSqlTableModel::OnFieldChange); m_sqlModel->select(); m_sqlModel->query().first(); connect(m_sqlModel, SIGNAL(beforeUpdate(int,QSqlRecord&)), this, SLOT(submitCheck(int,QSqlRecord&))); reOrderHeader(tableView->horizontalHeader()); }