Difference between revisions of "Qt/ModelView/ItemSelectionModel"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (Created page with '==main.cpp== <source lang="cpp"> #include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); r…') |
(No difference)
|
Latest revision as of 17:02, 21 December 2011
main.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); return app.exec(); }
form.h
#ifndef FORM_H #define FORM_H #include "ui_form.h" #include <QMainWindow> class QStringList; class QStringListModel; class QItemSelectionModel; class Form : public QMainWindow, private Ui::MainWindow { Q_OBJECT public: Form(QWidget *parent = 0); public slots: void slot_selectionChanged (const QItemSelection &selected, const QItemSelection &deselected ); protected: QStringListModel* Model; QItemSelectionModel* SelectionModel; QStringList* StringList; }; #endif
form.cpp
#include <QtGui> #include <iostream> #include "form.h" #include <QItemSelectionModel> #include <QStringListModel> Form::Form(QWidget *parent) : QMainWindow(parent) { setupUi(this); std::cout << "Current path: " << QDir::currentPath().toStdString() << std::endl; //this->StringList = new QStringList("a" << "b" << "c"); //this->StringList = new QStringList("a", "b", "c"); this->StringList = new QStringList; *(this->StringList) << "a"; *(this->StringList) << "b"; *(this->StringList) << "c"; this->Model = new QStringListModel; this->Model->setStringList(*this->StringList); this->listView->setModel(this->Model); this->SelectionModel = new QItemSelectionModel(this->Model); this->listView->setSelectionModel(this->SelectionModel); connect(this->SelectionModel, SIGNAL(selectionChanged (const QItemSelection&, const QItemSelection&)), this, SLOT(slot_selectionChanged(QItemSelection,QItemSelection))); } void Form::slot_selectionChanged (const QItemSelection &selected, const QItemSelection &deselected ) { for(unsigned int i = 0; i < selected.indexes().size(); ++i) { std::cout << selected.at(i).topLeft().row() << " "; } std::cout << std::endl; }
form.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>202</width> <height>248</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QListView" name="listView"/> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>202</width> <height>20</height> </rect> </property> <widget class="QMenu" name="menuFile"> <property name="title"> <string>File</string> </property> <addaction name="actionSave"/> </widget> <addaction name="menuFile"/> </widget> <widget class="QStatusBar" name="statusBar"/> <action name="actionSave"> <property name="text"> <string>Save</string> </property> </action> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ItemSelectionModel) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(ItemSelectionModel_UI form.ui) QT4_WRAP_CPP(ItemSelectionModel_MOC form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(ItemSelectionModel main.cpp form.h form.cpp ${ItemSelectionModel_UI} ${ItemSelectionModel_MOC}) TARGET_LINK_LIBRARIES(ItemSelectionModel ${QT_LIBRARIES})