Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QListView setItemDelegate problem sizeHint and paint not being called



  • Hey All,

    I am trying to implement this:
    https://forum.qt.io/topic/91480/implementing-chat-type-listview-with-text-bubbles

    If I do this, it works a treat::

    #include "mainwindow.h"
    #include "listviewdelegate.h"
    #include <QApplication>
    
    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    //	MainWindow w;
    //	w.show();
    
    	// create some data and put it in a model
    	QStandardItemModel mymodel;
    	QStandardItem *item1 = new QStandardItem("This is item one");
    	item1->setData("Incoming", Qt::UserRole + 1);
    	mymodel.appendRow(item1);
    	QStandardItem *item2 = new QStandardItem("This is item <b>two</b>, it is a very long item, but it's not the item's fault, it is me typing all this text.");
    	item2->setData("Outgoing", Qt::UserRole + 1);
    	mymodel.appendRow(item2);
    	QStandardItem *item3 = new QStandardItem("This is the third item");
    	item3->setData("Incoming", Qt::UserRole + 1);
    	mymodel.appendRow(item3);
    
    	// create a view and set our data
    	QListView listview;
    	listview.setResizeMode(QListView::Adjust);
    	listview.setWordWrap(true);
    	listview.setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    	listview.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	listview.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	listview.setModel(&mymodel);
    	listview.setMinimumSize(200,350);
    
    	// NOW tell the view to rely on out custom delegate for drawing
    	listview.setItemDelegate(new ListViewDelegate());
    
    	// show it
    	listview.show();
    
    	return a.exec();
    }
    

    However I don't want to use it this way, I want to uncomment the MainWindow lines:

    #include "mainwindow.h"
    #include "listviewdelegate.h"
    #include <QApplication>
    
    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	MainWindow w;
    	w.show();
    
    	// create some data and put it in a model
    //	QStandardItemModel mymodel;
    //	QStandardItem *item1 = new QStandardItem("This is item one");
    //	item1->setData("Incoming", Qt::UserRole + 1);
    //	mymodel.appendRow(item1);
    //	QStandardItem *item2 = new QStandardItem("This is item <b>two</b>, it is a very long item, but it's not the item's fault, it is me typing all this text.");
    //	item2->setData("Outgoing", Qt::UserRole + 1);
    //	mymodel.appendRow(item2);
    //	QStandardItem *item3 = new QStandardItem("This is the third item");
    //	item3->setData("Incoming", Qt::UserRole + 1);
    //	mymodel.appendRow(item3);
    
    //	// create a view and set our data
    //	QListView listview;
    //	listview.setResizeMode(QListView::Adjust);
    //	listview.setWordWrap(true);
    //	listview.setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    //	listview.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //	listview.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //	listview.setModel(&mymodel);
    //	listview.setMinimumSize(200,350);
    
    //	// NOW tell the view to rely on out custom delegate for drawing
    //	listview.setItemDelegate(new ListViewDelegate());
    
    //	// show it
    //	listview.show();
    
    	return a.exec();
    }
    

    And then do this in the MainWindow class creation:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "listviewdelegate.h"
    
    #include <QStandardItemModel>
    #include <QListView>
    
    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent),
    	ui(new Ui::MainWindow)
    {
    	ui->setupUi(this);
    
    	QStandardItemModel mymodel;
    	QStandardItem *item1 = new QStandardItem("This is item one");
    	item1->setData("Incoming", Qt::UserRole + 1);
    	mymodel.appendRow(item1);
    	QStandardItem *item2 = new QStandardItem("This is item <b>two</b>, it is a very long item, but it's not the item's fault, it is me typing all this text.");
    	item2->setData("Outgoing", Qt::UserRole + 1);
    	mymodel.appendRow(item2);
    	QStandardItem *item3 = new QStandardItem("This is the third item");
    	item3->setData("Incoming", Qt::UserRole + 1);
    	mymodel.appendRow(item3);
    
    	// create a view and set our data
    	ui->listview->setResizeMode(QListView::Adjust);
    	ui->listview->setWordWrap(true);
    	ui->listview->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    	ui->listview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	ui->listview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	ui->listview->setModel(&mymodel);
    	ui->listview->setMinimumSize(200,350);
    
    	// NOW tell the view to rely on out custom delegate for drawing
    	ui->listview->setItemDelegate(new ListViewDelegate);
    }
    
    MainWindow::~MainWindow()
    {
    	delete ui;
    }
    

    That is, I want to use the QListView I've created in QDesigner, but if I do this, sizeHint and paint never get called and I just get a blank QListView.

    Can someone help me please?

    Many thanks,

    Steve Q. :-)


  • Qt Champions 2019

    @steveq said in QListView setItemDelegate problem sizeHint and paint not being called:

    QStandardItemModel mymodel;

    C++ basics - how long do you think does this object lives?



  • Oh dear... that is a little embarrassing.

    Can't believe I looked over this countless times.

    Sorry to waste your time... thanks @Christian-Ehrlicher


  • Qt Champions 2019

    No problem, nice to get it solved :)


Log in to reply