Results 1 to 4 of 4

Thread: Need help with QAbstractItemView::setIndexWidget()

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Need help with QAbstractItemView::setIndexWidget()

    I want to filling the second column of my TableView with pushbuttons so I wrote the code below but the output is not what I want. Please see the attached snapshot of the output. As can been seen in the output window, the buttons are stacked at the top left-hand corner instead of being listed on the second column
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPushButton>
    4. #include <QStandardItemModel>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QStandardItemModel *model = new QStandardItemModel(41,2,this);
    13.  
    14.  
    15. for (int i = 0; i <= 41; i++)
    16. {
    17. QPushButton *btn = new QPushButton("E-Mail", this);
    18. ui->tableView->setIndexWidget(model->index(i, 2, QModelIndex()), btn);
    19. }
    20. ui->tableView->setModel(model);
    21. }
    To copy to clipboard, switch view to plain text mode 
    Cap_ture.PNG

  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: Need help with QAbstractItemView::setIndexWidget()

    Have you tried creating the buttons without parent?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with QAbstractItemView::setIndexWidget()

    Quote Originally Posted by anda_skoa View Post
    Have you tried creating the buttons without parent?
    _
    Yes I have, when I remove the parent on the buttons, the buttons disappear all together. At one point I did that and buttons appeared outside the window output. I also noticed the error in my code above, that I am pulling the model index from row i and column 3 when the model only has 2 columns. I changed that, as can be seen below, but the outcome is still the same.
    Qt Code:
    1. QModelIndex index = model->index(i, 1, QModelIndex());
    To copy to clipboard, switch view to plain text mode 
    Last edited by ayanda83; 23rd November 2016 at 10:00.

  4. #4
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with QAbstractItemView::setIndexWidget()

    Is there a problem with using QAbstractItemView::setIndexWidget() because every thread I've come across people recommend using a QItemDelegate instead even when what you really need is a QAbstractItemView::setIndexWidget(). Am I missing something in my code above, why doesn't it work?


    Added after 14 minutes:


    Never mind, I figured it out. You just have to set the model to the view before the loop.
    Last edited by ayanda83; 23rd November 2016 at 16:23.

Similar Threads

  1. Replies: 0
    Last Post: 3rd September 2011, 18:04
  2. setindexwidget and not selectable widget
    By ramazangirgin in forum Qt Programming
    Replies: 0
    Last Post: 12th January 2011, 07:28
  3. About setIndexWidget
    By aekilic in forum Qt Programming
    Replies: 20
    Last Post: 15th December 2009, 16:08
  4. having problem with setIndexWidget
    By hamid ghous in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 05:01
  5. setIndexWidget and proxy interaction
    By Derf in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2006, 18:15

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.