Results 1 to 2 of 2

Thread: Editing of Cell in QTableView with QComboBox as Editor

  1. #1
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Editing of Cell in QTableView with QComboBox as Editor

    Hi there,

    I can't get an editor for a cell in a table working. The table is displayed in a QTableView with a model implemented in a subclass of QAbstractTableModel. One column is to display a pen width parameter. This pen width is configured using a wizard. In the wizard a combo box is used which looks as follows:

    Pen Width.PNG

    The code for the model that is used by the combo box I found somewhere
    in the internet (and adapted it for my purposes). It looks as follows:

    Model class declaration:

    Qt Code:
    1. class PenWidthModel : public QAbstractListModel {
    2.  
    3. public:
    4. PenWidthModel(QObject *parent = nullptr);
    5.  
    6. public:
    7. int rowCount(const QModelIndex &parent) const override;
    8. QVariant data(const QModelIndex &index, int role) const override;
    9.  
    10. private:
    11. static void initialize();
    12.  
    13. static std::unique_ptr<QMap<int, QIcon>> thePenWidths2Icons;
    14. };
    To copy to clipboard, switch view to plain text mode 

    Model class implementation:

    Qt Code:
    1. unique_ptr<QMap<int, QIcon>>
    2. PenWidthModel::thePenWidths2Icons(nullptr);
    3.  
    4. PenWidthModel::PenWidthModel(QObject *parent) :
    5. if (thePenWidths2Icons.get() != nullptr) {
    6. return;
    7. }
    8. PenWidthModel::initialize();
    9. }
    10.  
    11. int PenWidthModel::rowCount(const QModelIndex &parent) const {
    12. if (parent.isValid())
    13. return 0;
    14.  
    15. return 5;
    16. }
    17.  
    18. QVariant PenWidthModel::data(const QModelIndex &index, int role) const {
    19. if (role == Qt::DecorationRole) {
    20. return (*thePenWidths2Icons)[index.row()];
    21. }
    22. return QVariant();
    23. }
    24.  
    25. void PenWidthModel::initialize() {
    26. thePenWidths2Icons = make_unique<QMap<int, QIcon>>();
    27. for (int i = 0; i < 6; ++i) {
    28. QPixmap pix(80, 14);
    29. pix.fill(Qt::white);
    30.  
    31. QBrush brush(Qt::black);
    32. QPen pen(brush, static_cast<double>(i + 1), Qt::SolidLine);
    33.  
    34. QPainter painter(&pix);
    35. painter.setPen(pen);
    36. painter.drawLine(2,7,78,7);
    37. painter.end();
    38. thePenWidths2Icons->insert(i, QIcon(pix));
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    As can be seen in the code for each of the available pen widths a QIcon object showing a pixmap with a line of appropriate thickness is provided (thickness of 0 is avoided).
    This model works fine in conjunction with the wizard and the combo box used in it for selection of a pen width.

    The pen width configured for a graph is later on shown in a table of graphs with their properties. In this table I want to be able to edit the pen width for a certain graph. In this context I want to reuse the PenWidthModel class. From my current understanding to edit data in a table in a non-standard way one has to use so called item delegates. So I implemented an item delegate that uses a QComboBox for editing of the pen width. My code looks as follows:

    Item delegate class declaration:

    Qt Code:
    1. class PenWidthDelegate : public QStyledItemDelegate {
    2. public:
    3. PenWidthDelegate(QObject *parent);
    4.  
    5. ~PenWidthDelegate();
    6.  
    7. public:
    8. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    9. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    10. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Item delegate class implementation:

    Qt Code:
    1. PenWidthDelegate::PenWidthDelegate(QObject *parent)
    2. : QStyledItemDelegate(parent) {
    3. }
    4.  
    5. PenWidthDelegate::~PenWidthDelegate() {
    6. }
    7.  
    8. QWidget *PenWidthDelegate::createEditor(QWidget *parent,
    9. const QStyleOptionViewItem &option,
    10. const QModelIndex &index) const {
    11. QComboBox *penWidthCB = new QComboBox(parent);
    12. penWidthCB->setModel(new PenWidthModel(parent));
    13. return penWidthCB;
    14. }
    15.  
    16. void PenWidthDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    17. QComboBox *penWidthCB = static_cast<QComboBox*>(editor);
    18. penWidthCB->setCurrentIndex(index.model()->data(index, Qt::EditRole).toInt());
    19. }
    20.  
    21. void PenWidthDelegate::setModelData(QWidget *editor,
    22. const QModelIndex &index) const {
    23. QComboBox *penWidthCB = static_cast<QComboBox*>(editor);
    24. int ix = penWidthCB->currentIndex();
    25. model->setData(index, ix, Qt::EditRole);
    26. }
    To copy to clipboard, switch view to plain text mode 

    As can be seen in the code in the createEditor(...) method I create the QComboBox instance to be used and assign an instance of the PenWidthModel class as its model. In the setEditorData(...) method I set the index of the to be shown pen width icon. and in the setModelData(...) method I write the selected pen width back to the table's model. But unfortunately the combo box shown when I initiate editing of a pen width does not display the icons representing the different pen widths. Instead it looks as follows:

    Pen Width Editor.PNG

    Obviously the icons provided by the model class are not displayed. Additionally the result of an edit is not immediately shown but seems to be shown only after one has clicked somewhere besides the edited cell.

    Obviously I'm doing something wrong ...
    Any help would be highly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Editing of Cell in QTableView with QComboBox as Editor

    Obviously the icons provided by the model class are not displayed.
    Its hard to tell from the posted screenshots, but to me it DOES seems to show the icons with the various line widths albeit it seems the margins of the content are thicker (so the icons seem to be "squashed", but I see the lines so maybe I don't understand what you mean?
    Could you try to elaborate on what you see vs. what you expect?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 0
    Last Post: 10th May 2017, 10:20
  2. QTableWidget how could I detect is a cell is editing??
    By webquinty in forum Qt Programming
    Replies: 1
    Last Post: 30th November 2012, 16:27
  3. Replies: 2
    Last Post: 28th October 2010, 10:26
  4. Replies: 1
    Last Post: 15th October 2010, 23:30
  5. Editing TableViews in UI Editor
    By emorgen in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 19:43

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.