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. QWidget and QTextEdit in QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

QWidget and QTextEdit in QAbstractTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 6.3k 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.
  • F Fuel

    As Example. My Widget has 5 Stars, its a Rating, and my Data for it is an int with 0-5. So my Model returns the int like a 3 and the ItemDelegate uses the QWidget to display the int value with 3 filled Stars? Is that what you mean?

    I hope i understand you right.

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #4

    @Fuel
    See the star delegate example.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    2
    • F Offline
      F Offline
      Fuel
      wrote on last edited by
      #5

      That Example is to complicated for my needs. I dont need it Editable and i have a Class that inherits from a QWidget. Dont know why its so complicated to put the Widget in this Cell. I go and try some Parts from that Example.

      raven-worxR 1 Reply Last reply
      0
      • F Fuel

        That Example is to complicated for my needs. I dont need it Editable and i have a Class that inherits from a QWidget. Dont know why its so complicated to put the Widget in this Cell. I go and try some Parts from that Example.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #6

        @Fuel
        antoher possibility is to use QAbstractItemView::setIndexWidget()
        But persistent editors / index widgets shouldn't be used for big lists.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Fuel
          wrote on last edited by
          #7

          i tried it, but nothing will be displayed

          ui->tvDiaryList->setIndexWidget(diarytreemodel->index(1, 3, QModelIndex()), new QPushButton);
          
          raven-worxR 1 Reply Last reply
          0
          • F Fuel

            i tried it, but nothing will be displayed

            ui->tvDiaryList->setIndexWidget(diarytreemodel->index(1, 3, QModelIndex()), new QPushButton);
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #8

            @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

            diarytreemodel->index(1, 3, QModelIndex())

            make sure the returned index is valid at the time you call this line of code -> means the model already has it's data set

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Fuel
              wrote on last edited by
              #9

              i decided to use a QStyledItemDelegate now. At the moment i dont know how i can paint the QWidget in the paint Event of the Delegate. This is what i have from a Tutorial, but its incomplete.

              void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
              {
                  if (index.data().canConvert<StarTable>())
                  {
                      StarTable starTable = qvariant_cast<StarTable>(index.data());
              
                      if (option.state & QStyle::State_Selected)
                          painter->fillRect(option.rect, option.palette.highlight());
              
                      starTable.paintEvent(); //dont know how to paint
                  }
                  else
                  {
                      QStyledItemDelegate::paint(painter, option, index);
                  }
              }
              
              raven-worxR 1 Reply Last reply
              0
              • F Fuel

                i decided to use a QStyledItemDelegate now. At the moment i dont know how i can paint the QWidget in the paint Event of the Delegate. This is what i have from a Tutorial, but its incomplete.

                void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
                {
                    if (index.data().canConvert<StarTable>())
                    {
                        StarTable starTable = qvariant_cast<StarTable>(index.data());
                
                        if (option.state & QStyle::State_Selected)
                            painter->fillRect(option.rect, option.palette.highlight());
                
                        starTable.paintEvent(); //dont know how to paint
                    }
                    else
                    {
                        QStyledItemDelegate::paint(painter, option, index);
                    }
                }
                
                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #10

                @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

                starTable.paintEvent(); //dont know how to paint

                -> starTable.render(painter);

                Although is your widget really refrenced by value? Meaning not a pointer?!
                I don't think that this even compiles.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Fuel
                  wrote on last edited by
                  #11

                  i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.

                  Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.

                  raven-worxR 2 Replies Last reply
                  0
                  • F Fuel

                    i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.

                    Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #12

                    @Fuel
                    you always get the index passed. Check the column (and/or row) of it.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Fuel
                      wrote on last edited by Fuel
                      #13

                      i know what you mean, but where?

                      update:

                      to show you something

                      DiaryTableModel *diarytablemodel = new DiaryTableModel(this);
                          StarDelegate *stardelegate = new StarDelegate(this);
                          ui->tvDiaryList->setModel(diarytablemodel);
                          ui->tvDiaryList->setItemDelegateForColumn(2, stardelegate);
                          ui->tvDiaryList->setSelectionBehavior(QAbstractItemView::SelectRows);
                      
                      void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
                      {
                          if (index.data().canConvert<StarTable*>())
                          {
                              StarTable *starTable = qvariant_cast<StarTable*>(index.data());
                      
                              starTable->render(painter);
                          }
                          else
                          {
                              QStyledItemDelegate::paint(painter, option, index);
                          }
                      }
                      
                      QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                      {
                          if (index.data().canConvert<StarTable*>())
                          {
                              StarTable *starTable = qvariant_cast<StarTable*>(index.data());
                              return starTable->sizeHint();
                          }
                          else
                          {
                              return QStyledItemDelegate::sizeHint(option, index);
                          }
                      }
                      
                      QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
                      {
                          if (!index.isValid())
                              return QVariant();
                      
                          if (role == Qt::DisplayRole)
                          {
                              if (index.column() == 0)
                                  return int(item.at(index.row())->Id);
                              else if (index.column() == 1)
                                  return QString(item.at(index.row())->Date);
                              else if (index.column() == 2)
                              {
                                  
                              }
                              else if (index.column() == 3)
                              {
                      
                              }
                          }
                      
                          return QVariant();
                      }
                      
                      void DiaryTableModel::setupModelData()
                      {
                          QTextEdit *text = new QTextEdit();
                          text->setText("Test");
                          item.append(new DiaryTableItem(0, "monday", new StarTable(2), text));
                      }
                      
                      1 Reply Last reply
                      0
                      • F Fuel

                        i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.

                        Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #14

                        @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

                        At the moment it doesnt work.

                        what exactly doesn't work?
                        If the column count is wrong, then your model needs to return the correct column count and data for the index.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          Fuel
                          wrote on last edited by
                          #15

                          It just dont shows. i get no Error or something

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            Fuel
                            wrote on last edited by
                            #16

                            Anyone can help me? Im so close. At the moment i get an Error. The Program stopps working because of a Read Access Violation. In the Debugger i can see that the starTable Pointer dont shows to a Memory Address

                            void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                       const QModelIndex &index) const
                            {
                            
                                    StarTable *starTable = qvariant_cast<StarTable*>(index.data());
                            
                                    starTable->render(painter);
                            
                            }
                            `The Pointer shows to 0x0. Anyone knows how to pick up here in this Delegate the Class that inherits from a QWidget?
                            raven-worxR 1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by SGaist
                              #17

                              Hi,

                              As @raven-worx wrote, are you sure the index is correct ? In any case, an assert for a null pointer wouldn't be a bad idea.

                              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
                              • F Fuel

                                Anyone can help me? Im so close. At the moment i get an Error. The Program stopps working because of a Read Access Violation. In the Debugger i can see that the starTable Pointer dont shows to a Memory Address

                                void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                           const QModelIndex &index) const
                                {
                                
                                        StarTable *starTable = qvariant_cast<StarTable*>(index.data());
                                
                                        starTable->render(painter);
                                
                                }
                                `The Pointer shows to 0x0. Anyone knows how to pick up here in this Delegate the Class that inherits from a QWidget?
                                raven-worxR Offline
                                raven-worxR Offline
                                raven-worx
                                Moderators
                                wrote on last edited by
                                #18

                                @Fuel
                                just a note: this thread also tries to achieve the same. You might want to follow it also.

                                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                If you have a question please use the forum so others can benefit from the solution in the future

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Fuel
                                  wrote on last edited by
                                  #19

                                  I solved that Problem. Sorry i forgot to mark the Thread. The Problem was the Index yes. In my data() Method of my TableModel i returned my Object wrong. So the Pointer was always null.

                                  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