Skip to content

Instantly share code, notes, and snippets.

@ksvbka
Last active February 19, 2020 01:34
Show Gist options
  • Select an option

  • Save ksvbka/789a35e8420d62a19a25d3a8aac9f596 to your computer and use it in GitHub Desktop.

Select an option

Save ksvbka/789a35e8420d62a19a25d3a8aac9f596 to your computer and use it in GitHub Desktop.
Two views onto the same model. TODO: implement selection model
qmake -project
# Add option by edit *.pro
# QT += core gui widgets
qmake
make
#include <QApplication>
#include <QWidget>
#include <QSplitter>
#include <QFileSystemModel>
#include <QTreeView>
#include <QListView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto spliter = new QSplitter();
auto model = new QFileSystemModel();
model->setRootPath(QDir::currentPath());
auto tree = new QTreeView(spliter);
tree->setModel(model);
tree->setRootIndex(model->index(QDir::currentPath()));
auto list = new QListView(spliter);
list->setModel(model);
list->setRootIndex(model->index(QDir::currentPath()));
// Set the selection model to sync selected item between 2 view
list->setSelectionModel(tree->selectionModel());
spliter->setWindowTitle("Two views onto the same model");
spliter->show();
return a.exec();
}
#include <QApplication>
#include <QWidget>
#include <QSplitter>
#include <QStringListModel>
#include <QComboBox>
#include <QListView>
int main(int argc, char *argv[]) {
QApplication app{argc, argv};
QSplitter splitter{Qt::Vertical};
auto model = new QStringListModel{&app};
model->setStringList(QStringList{"Gandalf", "Aragorn", "Legolas", "Samwise Gamgee" ,"Gimli", "Bilbo Baggins", "Peregrin Took", "Boromir"});
auto combo_box_view = new QComboBox{&splitter};
combo_box_view->setModel(model);
auto list_view = new QListView{&splitter};
list_view->setModel(model);
splitter.show();
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment