Difference between revisions of "Qt/ModelView/ComboBoxOfCheckBoxes"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (Created page with '==demo.cpp== <source lang="cpp"> #include <QApplication> #include "main.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; windo…') |
Daviddoria (Talk | contribs) |
||
| Line 17: | Line 17: | ||
==main.h== | ==main.h== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
| − | #ifndef | + | #ifndef MainWindow_H |
| − | #define | + | #define MainWindow_H |
#include <QMainWindow> | #include <QMainWindow> | ||
| Line 24: | Line 24: | ||
#include "ui_main.h" | #include "ui_main.h" | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
class MainWindow : public QMainWindow, public Ui::MainWindow | class MainWindow : public QMainWindow, public Ui::MainWindow | ||
{ | { | ||
| − | + | Q_OBJECT | |
public: | public: | ||
| − | + | MainWindow(QMainWindow *parent = 0); | |
| + | |||
| + | public slots: | ||
| + | |||
| + | //void slot_changed(); | ||
| + | void slot_changed(const QModelIndex&, const QModelIndex&); | ||
| + | |||
protected: | protected: | ||
| − | + | QStandardItemModel* Model; | |
| + | QStandardItem* Item1; | ||
| + | QStandardItem* Item2; | ||
| + | |||
| + | std::vector<QStandardItem*> Items; | ||
}; | }; | ||
| Line 49: | Line 51: | ||
==main.cpp== | ==main.cpp== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
| + | |||
#include <QtGui> | #include <QtGui> | ||
| Line 54: | Line 57: | ||
#include "main.h" | #include "main.h" | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
MainWindow::MainWindow(QMainWindow* parent) : QMainWindow(parent) | MainWindow::MainWindow(QMainWindow* parent) : QMainWindow(parent) | ||
{ | { | ||
setupUi(this); | setupUi(this); | ||
| − | + | this->Model = new QStandardItemModel; | |
| − | + | this->Item1 = new QStandardItem; | |
| − | + | ||
| − | + | this->Item1->setText("test"); | |
| − | + | this->Item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); | |
| − | + | this->Item1->setData(Qt::Unchecked, Qt::CheckStateRole); | |
| − | this->comboBox->setModel( | + | |
| − | + | this->Item2 = new QStandardItem; | |
| + | this->Item2->setText("test2"); | ||
| + | this->Item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); | ||
| + | this->Item2->setData(Qt::Unchecked, Qt::CheckStateRole); | ||
| + | |||
| + | connect(this->Model, SIGNAL(dataChanged ( const QModelIndex&, const QModelIndex&)), this, SLOT(slot_changed(const QModelIndex&, const QModelIndex&))); | ||
| + | |||
| + | this->Model->insertRow(0, this->Item1); | ||
| + | this->Model->insertRow(1, this->Item2); | ||
| + | |||
| + | this->Items.push_back(this->Item1); | ||
| + | this->Items.push_back(this->Item2); | ||
| + | |||
| + | this->comboBox->setModel(this->Model); | ||
| + | |||
std::cout << comboBox->model()->rowCount() << " rows after." << std::endl; | std::cout << comboBox->model()->rowCount() << " rows after." << std::endl; | ||
| + | } | ||
| + | |||
| + | void MainWindow::slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight) | ||
| + | { | ||
| + | //std::cout << "topLeft: " << topLeft.row() << std::endl; | ||
| + | //std::cout << "bottomRight: " << bottomRight.row() << std::endl; | ||
| + | std::cout << "Item " << topLeft.row() << " " << std::endl; | ||
| + | QStandardItem* item = this->Items[topLeft.row()]; | ||
| + | if(item->checkState() == Qt::Unchecked) | ||
| + | { | ||
| + | std::cout << "Unchecked!" << std::endl; | ||
| + | } | ||
| + | else if(item->checkState() == Qt::Checked) | ||
| + | { | ||
| + | std::cout << "Checked!" << std::endl; | ||
| + | } | ||
| + | |||
} | } | ||
Latest revision as of 08:43, 8 December 2011
demo.cpp
#include <QApplication> #include "main.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); }
main.h
#ifndef MainWindow_H #define MainWindow_H #include <QMainWindow> #include <QStandardItemModel> #include "ui_main.h" class MainWindow : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: MainWindow(QMainWindow *parent = 0); public slots: //void slot_changed(); void slot_changed(const QModelIndex&, const QModelIndex&); protected: QStandardItemModel* Model; QStandardItem* Item1; QStandardItem* Item2; std::vector<QStandardItem*> Items; }; #endif
main.cpp
#include <QtGui> #include <iostream> #include "main.h" MainWindow::MainWindow(QMainWindow* parent) : QMainWindow(parent) { setupUi(this); this->Model = new QStandardItemModel; this->Item1 = new QStandardItem; this->Item1->setText("test"); this->Item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); this->Item1->setData(Qt::Unchecked, Qt::CheckStateRole); this->Item2 = new QStandardItem; this->Item2->setText("test2"); this->Item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); this->Item2->setData(Qt::Unchecked, Qt::CheckStateRole); connect(this->Model, SIGNAL(dataChanged ( const QModelIndex&, const QModelIndex&)), this, SLOT(slot_changed(const QModelIndex&, const QModelIndex&))); this->Model->insertRow(0, this->Item1); this->Model->insertRow(1, this->Item2); this->Items.push_back(this->Item1); this->Items.push_back(this->Item2); this->comboBox->setModel(this->Model); std::cout << comboBox->model()->rowCount() << " rows after." << std::endl; } void MainWindow::slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight) { //std::cout << "topLeft: " << topLeft.row() << std::endl; //std::cout << "bottomRight: " << bottomRight.row() << std::endl; std::cout << "Item " << topLeft.row() << " " << std::endl; QStandardItem* item = this->Items[topLeft.row()]; if(item->checkState() == Qt::Unchecked) { std::cout << "Unchecked!" << std::endl; } else if(item->checkState() == Qt::Checked) { std::cout << "Checked!" << std::endl; } }
main.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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>240</x>
<y>150</y>
<width>211</width>
<height>27</height>
</rect>
</property>
<item>
<property name="text">
<string>Item1</string>
</property>
</item>
<item>
<property name="text">
<string>Item2</string>
</property>
</item>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ComboBoxOfCheckBoxes) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) QT4_WRAP_UI(UISrcs main.ui) QT4_WRAP_CPP(MOCSrcs main.h) ADD_EXECUTABLE(ComboBoxOfCheckBoxes demo.cpp main.h main.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(ComboBoxOfCheckBoxes ${QT_LIBRARIES})