Skip to content

Instantly share code, notes, and snippets.

@papylhomme
Created March 27, 2015 23:25
Show Gist options
  • Save papylhomme/c2105b85c80650b0c3ea to your computer and use it in GitHub Desktop.
Save papylhomme/c2105b85c80650b0c3ea to your computer and use it in GitHub Desktop.
Test QListView using grid layout and dataChanged signal
#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