Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Existend subclasses of QAbstractItemDelegate in the Qt-Lib
Forum Updated to NodeBB v4.3 + New Features

Existend subclasses of QAbstractItemDelegate in the Qt-Lib

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by
    #1

    Hello !
    I fiqure out how to work with ItemDelgates. My idea is, before I begin with my own ItemDelegates from the scratch (with Painting etc.) I subclass some existent and change only some rules. This is enough for my current task.
    For example the SqlTableModel uses some automatical: Text, Comboboxes and Spinwidgets.
    The problem is I cannot find out what are the names of this special ItemDelegates to subclass from it. Maybe Qt has some other usefull ItemDelegates integrated yet, which I dont know and cannot active.

    Andreas

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi,
      take a look at this piece of documentation: http://doc.qt.io/qt-5/qitemeditorfactory.html. Once you've created and registered your own editor factory you can use it for your delegate with: void QItemDelegate::setItemEditorFactory(QItemEditorFactory * factory).

      1 Reply Last reply
      0
      • Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by
        #3

        Ok, I understand much more from Qt now. The Itemeditorfactory is an other elegant way to manage the editor depent on the value type.
        Unfortunately, this solves not my problem. I have a Bool column in the Access database, but it has is been delivered by ODBC as an unsignet int. So I get a Spin control.
        I want a checkbox or a color indicator. Manage all uints same I dont like. Therfore I must set a concrete Itemdelegate for this column. It seem that I must implement my itemdelegate from scratch.

        ? 1 Reply Last reply
        0
        • Andy314A Andy314

          Ok, I understand much more from Qt now. The Itemeditorfactory is an other elegant way to manage the editor depent on the value type.
          Unfortunately, this solves not my problem. I have a Bool column in the Access database, but it has is been delivered by ODBC as an unsignet int. So I get a Spin control.
          I want a checkbox or a color indicator. Manage all uints same I dont like. Therfore I must set a concrete Itemdelegate for this column. It seem that I must implement my itemdelegate from scratch.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          In this case I suggest not to implement a custom table widget but to place a translator between the data provider and the table widget that casts the unsigned int fields to bool and the other way round.

          1 Reply Last reply
          0
          • Andy314A Offline
            Andy314A Offline
            Andy314
            wrote on last edited by
            #5

            Thank you for you answer, but the way to set the Edit/Display-Widget only by value type is to unflexible for me. I need different displays for the same type.
            For example different images for diffrent bool columns I see now.

            I am a litttle bit confuse about QItemDelegate and QStyledItemDelegate.
            Which I shall use? I need felxible painting!

            The Styled version has a complete different painting interface (precise none - it goes over the Application) . The most examples I found in the internet and the my book are about the unstyled version.
            What is recommended?

            Nevertheless, nothing works. I want a simple red background.

            void TBoolItDel::paint(QPainter* painter, const QStyleOptionViewItem& opt,
            const QModelIndex& index) const
            {
            QStyleOptionViewItem so=opt;
            so.backgroundBrush.setColor(Qt::red);
            drawDisplay(painter, so, so.rect, OnText);
            }

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #6

              I'm not an expert on this, but the documentation states that QStyledItemDelegate was added to Qt after the original QItemDelegate and that the only difference is that QStyledItemDelegate gives you access to the QStyle currently used by the application. In other words: One should use QStyledItemDelegate for better platform integration.

              I've found a complete example in the official docs: http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html.

              Hope this helps!

              1 Reply Last reply
              0
              • Andy314A Offline
                Andy314A Offline
                Andy314
                wrote on last edited by
                #7

                The painting I can manage now.
                Now I have a problem with the editor creation. I create a checkbox widget and I can see it but it overwrites not the background from the paininting. So I get a confuse text mix. updateEditorGeometry is triggered.

                void TBoolItemDelegate::updateEditorGeometry(QWidget *editor,
                    const QStyleOptionViewItem &option, const QModelIndex & index) const
                {
                    editor->setGeometry(option.rect);
                }
                

                What must I do to create a full widget in full cell-size. Must I put in in an Layout ?

                If found this code

                class BooleanWidget : public QWidget
                {
                    Q_OBJECT
                    QCheckBox * checkBox;
                    public:
                    BooleanWidget(QWidget * parent = 0)
                    {
                        checkBox = new QCheckBox(this);
                        QHBoxLayout * layout = new QHBoxLayout(this);
                        layout->addWidget(checkBox, 0, Qt::AlignCenter);
                    }
                };
                

                When I take this widget a separate window pops up out of the cell ?

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Please show us your reimplementation of QWidget * QStyledItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const.

                  Btw: Have you already seen the spin box delegate example ( http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html ) ?

                  1 Reply Last reply
                  0
                  • Andy314A Offline
                    Andy314A Offline
                    Andy314
                    wrote on last edited by
                    #9

                    Here is my simple Editorcreator:

                    QWidget *TBoolItemDelegate::createEditor(QWidget *parent,
                        const QStyleOptionViewItem & opt ,
                        const QModelIndex &index ) const
                    {
                        bool on = index.model()->data(index, Qt::EditRole).toBool();
                        QCheckBox *c = new QCheckBox(parent);
                        c->setChecked(on);
                        c->setText(OnText);
                        return c;
                     }
                    

                    An additional effect, which gives a hint on the bug maybe:
                    My paint function draws the text in different colors for on an off,
                    but when the editor opens, the text in the backgroud gets a black text color.
                    Therefore an additional drawing has been performed.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved