QTainModel似乎没有在QMainWindow上显示

时间:2012-04-11 20:01:40

标签: c++ qt

以下是我的MainWindow的代码:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QSqlDatabase connection = QSqlDatabase::addDatabase("QMYSQL");
    connection.setHostName("localhost");
    connection.setDatabaseName("dbname");
    connection.setUserName("username");
    connection.setPassword("Temp");



    QTableView *view = new QTableView(this);

    view->setMinimumSize(200, 200);

    QSqlTableModel *model = new QSqlTableModel(this, connection);
    model->setTable("users");
    qDebug()  << model->columnCount() << model->rowCount();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->removeColumn(0); // don't show the ID



    view->setModel(model);

    view->show();

    this->setWindowTitle("Showing Things");
}

我的main.cpp

#include "mainwindow.h"

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

知道我的数据库包含数据,我基本上遵循详细解释here中的示例。

这是我编译和运行时得到的结果:

nada

当我期待这样的事情时:

what I want

得到它。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

根据您发布的代码,您没有打开数据库连接。

QSqlTableModel无法为您打开它。