我有一个QTableView,其中填充了QSqlQueryModel。我试图根据检查哪个RadioButton对表进行排序,但是当我按下它们时什么也没发生。有一点我可以让它排序,但只有一次。我在这里做错了什么?
void MainWindow::on_openButton_clicked()
{
QString filePath = ui->lineEdit->text();
if( filePath != "" ){
if( openDB( filePath ) ){
ui->debugLabel->setText("Database opened");
MainWindow::populateTable();
}else{
ui->debugLabel->setText("Unable to open database");
}
} else {
ui->debugLabel->setText("Path is empty");
}
}
void MainWindow::populateTable(){
if( readDB() ){
ui->tableView->setModel(toast.dbModel);
}
}
void MainWindow::on_shootButton_toggled(bool checked)
{
if( checked ){
ui->tableView->sortByColumn( 0 );
}
}
void MainWindow::on_winButton_toggled(bool checked)
{
if( checked ){
ui->tableView->sortByColumn( 1 );
}
}
bool openDB(QString path){
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
return db.open();
}
bool readDB(){
if( db.isOpen() ){
dbModel->setQuery( "select * from test", db );
return true;
} else {
return false;
}
}
答案 0 :(得分:-1)
QSqlQueryModel
默认情况下无法排序,QSqlTableModel
可排序,但两者之间存在差异。您可以通过继承该类并重新实现QSqlQueryModel
方法来对sort()
进行排序,如果您查看QAbstractItemModel
的文档,它会为您提供有关它的详细信息。