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. Delegate

Delegate

Scheduled Pinned Locked Moved Unsolved General and Desktop
delegate
7 Posts 2 Posters 2.0k Views 1 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.
  • W Offline
    W Offline
    WhiteBeard
    wrote on last edited by
    #1

    I have a simple MVC architecture with a QStandardItemModel and a QtableView.

    I would like to define special behaviors for all items in column 1 and I do not know how to do it.

    #include "test.h"
    #include"modeltest.h"
    //Test::Test(QWidget *parent) : QWidget(parent) {}
     
     
    /*
     *the Classe that  contains
     * the tabView
     */
     
    Test::Test() : QWidget()
    {
         QStandardItemModel *model = new QStandardItemModel(0, 2);
        QStringList list;
        list << "france" << "Angleterre" << "Allemagne"  << "Italie";
        for(int row = 0; row<4; ++row)
        {
            model->setItem(row, 0, new QStandardItem(static_cast<QString>(list.at(row))));
        }
        QTableView *table = new QTableView;
        table->setModel(model);
        ModelTest *delegate;
        table->setItemDelegateForColumn(0, delegate);
        QVBoxLayout *vlayout = new QVBoxLayout;
        vlayout->addWidget(table);
        setLayout(vlayout);
     }
     
     
     
     
    /*Header of the class
     * that handle the delegate 
     */
     
     
     
    #ifndef MODELTEST_H
    #define MODELTEST_H
     
    #include <QtWidgets>
    #include<QObject>
     
    class ModelTest : public QItemDelegate
    {
        Q_OBJECT
    public:
        ModelTest(QObject *parent = 0);
     
    public:
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        void setEditorData(QWidget *editor, const QModelIndex &index) const;
    //    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
        void updateEditorGeometry(QWidget *editor, QStyleOptionViewItem &option, const QModelIndex &index) const;
    };
     
    #endif // MODELTEST_H
     
     
     
     
     
     
     
    /*.cpp of the classe 
     *that handle the  
     *delegate
     */
     
    #include "modeltest.h"
     
    ModelTest::ModelTest(QObject *parent) : QItemDelegate(parent)
    {
    }
     
    QWidget *ModelTest::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
     
        QSplitter *splitter = new QSplitter;
     
        return splitter;
     
    }
    void ModelTest::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QStringList list;
        QStringList list2;
        list<< "cameroun" << "tchad" << "congo";
        list2 << "ivoire" << "argent" << "pain";
        QStandardItemModel *model = new QStandardItemModel;
        QStandardItem *parent1 = new QStandardItem("Pays");
        for(int row =0; row<3; ++row)
        {
            parent1->appendRow(new QStandardItem(static_cast<QString>(list.at(row))));
        }
        QStandardItem *parent2 = new QStandardItem("Monnaie");
        for(int row =0; row<3; ++row)
        {
            parent2->appendRow(new QStandardItem(static_cast<QString>(list2.at(row))));
        }
        model->setItem(0, 0, parent1);
        model->setItem(1, 0, parent1);
        QTreeView *tree = new QTreeView(static_cast<QSplitter*>(editor));
        tree->setModel(model);
    }
    void ModelTest::updateEditorGeometry(QWidget *editor, QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        editor->setGeometry(option.rect);
     
    }
    

    In fact the code compiles and executes but nothing happens as if I had not defined any delegate.

    How can I do? Thank you for answering me

    1 Reply Last reply
    0
    • W Offline
      W Offline
      WhiteBeard
      wrote on last edited by
      #2

      Hi,

      What I really wanted to do is that. I fill both columns with a tab view with strings (in my case here I
      filled only one column but it has no impact) then I modify the delegates only for the 1st column using setItemColumnDelegate. The modified delegate must (on the current index) make disappear the string that is in the index (make it invisible with setVisible (false)) then make appear a splitter (widget inheriting a frame therefore same style as the frame ) That contains two tree views of other strings

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi,

        Just to be sure I understand you correctly. You want your delegate to show two QTreeViews put in a QSlider QSplitter whenever you select a cell from your QTableView ?

        [edit: fixed question SGaist]

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        W 1 Reply Last reply
        0
        • W Offline
          W Offline
          WhiteBeard
          wrote on last edited by
          #4

          Yes but no QSlider but QSplitter. thanks to answering me :)

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Just to be sure I understand you correctly. You want your delegate to show two QTreeViews put in a QSlider QSplitter whenever you select a cell from your QTableView ?

            [edit: fixed question SGaist]

            W Offline
            W Offline
            WhiteBeard
            wrote on last edited by
            #5

            @SGaist Yes

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WhiteBeard
              wrote on last edited by
              #6

              I have solved my problem. thanks for your attention :)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Great !

                What did you implement ?

                Also, since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum uses may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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