Название: Не отображается внешнее представление
Отправлено: codenameRazuka от Октябрь 31, 2015, 18:55
Помогите. Пытаюсь вывести содержимое таблицы базы данных в таблице. #include <QApplication> #include <QtSql> #include <QTableView> #include <QTableWidget> #include <QSqlQuery>
int main(int argc, char *argv[]) { QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "mydb"); db.setHostName("localhost"); db.setDatabaseName("myforum"); db.setUserName("root"); db.setPassword("123asd"); db.open();
QSqlQuery* query = new QSqlQuery(db);
query->exec("create table group_ (" "group_id tinyint unsigned auto_increment," "group_name char(32) not null," "primary key (group_id)," "unique (group_name)" ")" );
query->exec("create table section (" "section_id tinyint unsigned auto_increment," "section_name char(32) not null," "primary key (section_id)," "unique (section_name)" ")" );
query->exec("create table user (" "user_id int unsigned auto_increment," "user_nickname char(32) not null," "user_email char(255) not null," "user_password char(40) not null," "user_date_of_registration datetime not null," "user_group tinyint unsigned not null," "primary key (user_id)," "foreign key (user_group) references group_ (group_id)," "unique (user_nickname, user_email)" ")" );
query->exec("create table forum (" "forum_id smallint unsigned auto_increment," "forum_name char(32) not null," "forum_section tinyint unsigned not null," "primary key (forum_id)," "foreign key (forum_section) references section (section_id)," "unique (forum_name)" ")" );
query->exec("create table topic (" "topic_id mediumint unsigned auto_increment," "topic_name char(64) not null," "topic_author int unsigned not null," "topic_forum smallint unsigned not null," "topic_date datetime not null," "primary key (topic_id)," "foreign key (topic_author) references user (user_id)," "foreign key (topic_forum) references forum (forum_id)" ")" );
query->exec("create table comment (" "comment_id int unsigned auto_increment," "comment_author int unsigned not null," "comment_date datetime not null," "comment_topic mediumint unsigned not null," "comment_contents text not null," "primary key (comment_id)," "foreign key (comment_author) references user (user_id)," "foreign key (comment_topic) references topic (topic_id)" ")" );
query->exec("insert into group_ (group_name) values ('administrator')"); query->exec("insert into group_ (group_name) values ('moderator')"); query->exec("insert into group_ (group_name) values ('user')"); query->exec("insert into group_ (group_name) values ('guest')"); query->exec("insert into group_ (group_name) values ('administrator')"); query->exec("insert into group_ (group_name) values ('banned')");
QSqlTableModel* model = new QSqlTableModel();
model->setTable("group_"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Group Name"));
QTableView *view = new QTableView; view->setModel(model); view->setWindowTitle("It kinda works!!!");
view->show();
return a.exec(); }
Однако, вместо таблицы, вижу белое окно. В чём моя ошибка? Примеры, естественно, работают, и я делаю так же как и в примерах.
Название: Re: Не отображается внешнее представление
Отправлено: Vamireh от Октябрь 31, 2015, 20:25
Должно быть: QSqlTableModel* model = new QSqlTableModel(0, db);
Или даже: QSqlTableModel* model = new QSqlTableModel(view, db);
Название: Re: Не отображается внешнее представление
Отправлено: codenameRazuka от Октябрь 31, 2015, 21:07
Должно быть: QSqlTableModel* model = new QSqlTableModel(0, db);
Или даже: QSqlTableModel* model = new QSqlTableModel(view, db);
Спасибо, уже разобрался. :)
|