我有以下计划:
mainwindow.h:
#include <QDialog>
#include <QListView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QVBoxLayout>
class MainWindow : public QDialog {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QVBoxLayout layout;
QListView view1, view2, view3;
QStandardItemModel model1, model2, model3;
QStandardItem item1, item2, item3, item4, item5, item6;
};
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent),
item1 {"Item 1"},
item2 {"Item 2"},
item3 {"Item 3"},
item4 {"Item 4"},
item5 {"Item 5"},
item6 {"Item 6"}
{
model1.appendColumn({&item1, &item2});
model2.appendColumn({&item3, &item4});
model3.appendColumn({&item5, &item6});
view1.setModel(&model1);
view2.setModel(&model2);
view3.setModel(&model3);
layout.addWidget(&view1);
layout.addWidget(&view2);
layout.addWidget(&view3);
this->setLayout(&layout);
connect(view1.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view2.selectionModel(), SLOT(reset()));
connect(view1.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view3.selectionModel(), SLOT(reset()));
connect(view2.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view1.selectionModel(), SLOT(reset()));
connect(view2.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view3.selectionModel(), SLOT(reset()));
connect(view3.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view2.selectionModel(), SLOT(reset()));
connect(view3.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
view1.selectionModel(), SLOT(reset()));
}
main.cpp中:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.exec();
return a.exec();
}
a.pro
TEMPLATE = app
TARGET = a
INCLUDEPATH += .
QT += widgets
CONFIG += c++11
HEADERS += mainwindow.h
SOURCES += main.cpp mainwindow.cpp
这是我尝试创建一个QDialog
个QListViews
,其中QListView
具有不同的模型,其中只能同时选择一个项目。如果您只是单击不同的项目,它就可以工作,但是一旦您在空白区域中单击QListView
,然后单击该项目,就会选择多个条目。然后,只有在空白区域中单击其他slots
或最小化对话框并再次激活它时,才会清除选择。
我错过了什么?我是否必须连接其他signals
/ <li class="noUnderline><a href="#">
?