Results 1 to 7 of 7

Thread: QListView within a plugin that is enabled/disabled: doesn't show?

  1. #1
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    Hello,

    I'm using a custom Qt plugin with and API another team gave me. My understanding problem is about data binding and UI refresh.

    The small plugin I use gets instantiated when I open the main GUI, then if I click on a menu item it gets enabled (visible).

    My QListView doesn't show, I see an empty rounded frame with a border so there is some empty widget.

    So here is some code to check what are coding guidelines I miss...

    Thank you very much.

    Qt Code:
    1. // constructor that gets called when opening up main GUI
    2. AppletTutorial2Imp::AppletTutorial2Imp(QObject *parent) : XApplet(parent) {
    3. m_enabled = false;
    4. // Set up main widget
    5. m_mainWidget = new QWidget();
    6. m_mainWidget->setLayout(new QHBoxLayout());
    7.  
    8. m_listview = new QListView();
    9. m_mainWidget->layout()->addWidget(m_listview);
    10.  
    11. // Assign main widget for applet to display
    12. setWidget(m_mainWidget);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // code that gets called when choosing a menu item to display a specific module (plugin)
    2. void AppletTutorial2Imp::setEnable(bool enable) {
    3.  
    4. if(m_enabled != enable) {
    5. m_enabled = enable;
    6. }
    7.  
    8. if(m_enabled) {
    9. listDicomImages();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Code that feeds QListView model and assign model to QListView
    2. void AppletTutorial2Imp::listDicomImages() {
    3.  
    4. DicomImageModel model;
    5. foreach... {
    6. // Get object from memory query then:
    7. model.addImage(image);
    8. }
    9. m_listview->setModel(&model);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 


    Added after 15 minutes:


    OK it seems to behave better if in my model class I have data retrieval that works. When I get an exception accessing non existing data it gets trapped. So I have more logs (I removed log statements from code pasted here) but still blank list view.

    In my model I implemented only the following:

    Qt Code:
    1. // constructor, destructor, then:
    2.  
    3. int DicomImageModel::rowCount(const QModelIndex & /*parent*/) const
    4. {
    5. return m_dicomImages.size();
    6. }
    7.  
    8. int DicomImageModel::columnCount(const QModelIndex & /*parent*/) const
    9. {
    10. return 6;
    11. }
    12.  
    13. QVariant DicomImageModel::data(const QModelIndex &index, int role) const
    14. {
    15. int row = index.row();
    16. int col = index.column();
    17.  
    18. if(role == Qt::DisplayRole){
    19. if (col == 0) return QString(m_dicomImages[row]->getId());
    20. if (col == 1) return QString(m_dicomImages[row]->getDicomRef().getSeriesInstanceUID());
    21.  
    22. return QString("Row%1, Column%2")
    23. .arg(row + 1)
    24. .arg(col +1);
    25. }
    26.  
    27. return QVariant();
    28. }
    29.  
    30. void DicomImageModel::addImage(const XImageObjectPtr image)
    31. {
    32. m_dicomImages.push_back(image);
    33. }
    To copy to clipboard, switch view to plain text mode 

    I changed the add item handling method in the model but list remains empty... so where is it wrong??

    Qt Code:
    1. void DicomImageModel::addImage(const XImageObjectPtr image)
    2. {
    3. beginInsertRows(QModelIndex(), m_dicomImages.size(), m_dicomImages.size());
    4. m_dicomImages.push_back(image);
    5. endInsertRows();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by postb99; 18th February 2014 at 10:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    Quote Originally Posted by postb99 View Post
    Qt Code:
    1. // Code that feeds QListView model and assign model to QListView
    2. void AppletTutorial2Imp::listDicomImages() {
    3.  
    4. DicomImageModel model;
    5. foreach... {
    6. // Get object from memory query then:
    7. model.addImage(image);
    8. }
    9. m_listview->setModel(&model);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    If this is your actual code, then the model gets destroyed at the end of the method (variable "model" goes out of scope) and m_listview is now working with an invalid pointer.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    postb99 (18th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    Now my model is correct but my list is empty. So I still miss something, I guess my model updates aren't correctly notified to QListView?
    Do I need to set some properties of list or can I use default "new QListView()"?

    My model is now a member of plugin class.

    I connected the plugin to a signal so that it does:

    Qt Code:
    1. m_model.addImage(obj.dynamicCast<XImageObject>());
    To copy to clipboard, switch view to plain text mode 

    Called code:

    Qt Code:
    1. void DicomImageModel::addImage(const XImageObjectPtr image)
    2. {
    3. int nbRows = m_dicomImages.size();
    4. beginInsertRows(QModelIndex(), nbRows, nbRows);
    5. m_dicomImages.push_back(image);
    6. xLogWarning(XLogRecord::Type_User, QString(tr("Image %1 added").arg(image->getId())));
    7. endInsertRows();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    The looks ok.
    I assume you are deriving from QAbstractTableModel?

    Do you rowCount() and columnCount() methods get called?

    Cheers,
    _

  6. #5
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    I indeed derive from QAbstractTableModel.

    You got it, something is wrong with rowCount() and columnCount(), they're called at startup, when model is instantiated.

    Then when I add an image to model, rowCount() gets called, but shows "0 row", then I have "1 object in model".

    When I then click onto menu item link, my widget shows, but neither rowCount() nor columnCount() get called again...

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    Hmm.

    Make sure you are not having multiple instances of the model, i.e. that the model you are adding images to is the one that is set on the view.

    Also try to verify the correct operations yourself, e.g.

    Qt Code:
    1. qDebug() << "row count, emtpy model" << m_model->rowCount();
    2.  
    3. // add image
    4.  
    5. qDebug() << "row count, one image" << m_model->rowCount();
    6.  
    7. QModelIndex index = m_model->index(0, 0); // index for first image, id column
    8. qDebug() << "index.isValid=" << index.isValid();
    9.  
    10. qDebug() << "image id=" << index.data(Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    See also http://qt-project.org/wiki/Model_Test

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    postb99 (19th February 2014)

  9. #7
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QListView within a plugin that is enabled/disabled: doesn't show?

    Your guess was right, I was reinstantiating the model (half correction of wrong code)

Similar Threads

  1. QListView - To select a disabled item
    By agarny in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2011, 02:23
  2. Replies: 1
    Last Post: 20th April 2011, 22:51
  3. Navigation by QListView with enabled isWrapping property
    By qt-closer in forum Qt Programming
    Replies: 0
    Last Post: 12th October 2010, 22:04
  4. Need to show a menu item with disabled look but functional
    By lalesculiviu in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 16:16
  5. Replies: 0
    Last Post: 19th March 2009, 14:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.