Created
March 27, 2015 23:25
-
-
Save papylhomme/c2105b85c80650b0c3ea to your computer and use it in GitHub Desktop.
Test QListView using grid layout and dataChanged signal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QMainWindow> | |
#include <QLayout> | |
#include <QPushButton> | |
#include <QListView> | |
#include <QStringListModel> | |
class CustomModel : public QStringListModel { | |
Q_OBJECT | |
public: | |
CustomModel() { | |
QStringList l; | |
l << "this is a long word"; | |
l << "another word"; | |
setStringList(l); | |
} | |
public slots: | |
void testUpdate() { | |
QModelIndex index = createIndex(0, 0); | |
emit dataChanged(index, index); | |
} | |
}; | |
#include "testlist.moc" | |
int main(int argc, char *argv[]) { | |
QApplication app(argc, argv); | |
QMainWindow* w = new QMainWindow(); | |
QWidget* mainWidget = new QWidget(); | |
QBoxLayout* layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom); | |
QPushButton* button = new QPushButton(); | |
button -> setText("Update"); | |
CustomModel* model = new CustomModel(); | |
QObject::connect(button, SIGNAL(clicked()), model, SLOT(testUpdate())); | |
QListView* list = new QListView(); | |
list -> setMovement(QListView::Static); | |
list -> setResizeMode(QListView::Adjust); | |
list -> setViewMode(QListView::IconMode); | |
list -> setGridSize(QSize(30, 20)); | |
list -> setWrapping(true); | |
list -> setWordWrap(true); | |
list -> setModel(model); | |
layout -> addWidget(button); | |
layout -> addWidget(list); | |
mainWidget -> setLayout(layout); | |
w -> setCentralWidget(mainWidget); | |
w -> show(); | |
return app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment