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. QTableView add Rows renders wrong
Forum Updated to NodeBB v4.3 + New Features

QTableView add Rows renders wrong

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 3 Posters 8.7k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 21 Aug 2017, 19:39 last edited by
    #2

    Hi,

    You should share how you create that QTableView as well as the configuration you use for it.

    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
    1
    • F Offline
      F Offline
      Fuel
      wrote on 22 Aug 2017, 04:32 last edited by
      #3
      doctorContactModel = new DoctorContactModel(this);
          DoctorContactDelegate *doctorContactDelegate = new DoctorContactDelegate(this);
          ui->tvDoctorContact->setModel(doctorContactModel);
          ui->tvDoctorContact->setItemDelegate(doctorContactDelegate);
          ui->tvDoctorContact->setSelectionMode(QAbstractItemView::SingleSelection);
          ui->tvDoctorContact->setEditTriggers(QAbstractItemView::SelectedClicked);
          ui->tvDoctorContact->resizeColumnToContents(0);
          ui->tvDoctorContact->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
          ui->tvDoctorContact->horizontalHeader()->setStretchLastSection(true);
      

      I didnt changed something in the Configuration of the QTableView. Only the Objectname. But how can i post the Configuration?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 22 Aug 2017, 19:23 last edited by
        #4

        That looks good.

        What is your DoctorContactModel ?

        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 Offline
          F Offline
          Fuel
          wrote on 22 Aug 2017, 21:28 last edited by Fuel
          #5

          @SGaist said in QTableView add Rows renders wrong:

          DoctorContactModel

          Thats a Model Class from QAbstractTableModel. I can post the code.

          #include "doctorcontactmodel.h"
          #include "database.h"
          
          DoctorContactModel::DoctorContactModel(QObject *parent)
              : QAbstractTableModel(parent)
          {
          }
          
          DoctorContactModel::~DoctorContactModel()
          {
              for (auto item : mItems)
              {
                  delete item;
              }
          }
          
          QVariant DoctorContactModel::headerData(int section, Qt::Orientation orientation, int role) const
          {
              if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
              {
                  switch (section)
                  {
                      case 0:
                          return QString("ID");
                      case 1:
                          return QString("Kontakt");
                      case 2:
                          return QString("Kategorie");
                      case 3:
                          return QString("Kommentar");
                  }
              }
          
              return QVariant();
          }
          
          int DoctorContactModel::rowCount(const QModelIndex &parent) const
          {
              if (parent.isValid())
                  return 0;
          
              return mItems.count();
          }
          
          int DoctorContactModel::columnCount(const QModelIndex &parent) const
          {
              if (parent.isValid())
                  return 0;
          
              return 4;
          }
          
          QVariant DoctorContactModel::data(const QModelIndex &index, int role) const
          {
              if (!index.isValid())
                  return QVariant();
          
              if (role == Qt::DisplayRole)
              {
                  if (index.column() == 0)
                      return mItems.at(index.row())->Id;
                  if (index.column() == 1)
                      return mItems.at(index.row())->Value;
                  if (index.column() == 3)
                      return mItems.at(index.row())->Comment;
              }
          
              if (index.column() == 2)
                  return QVariant::fromValue(mItems.at(index.row())->Type);
          
              return QVariant();
          }
          
          Qt::ItemFlags DoctorContactModel::flags(const QModelIndex &index) const
          {
              return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
          }
          
          bool DoctorContactModel::setData(const QModelIndex &index, const QVariant &value, int role)
          {
              Q_UNUSED(role);
          
              if (index.column() == 1)
                  mItems.value(index.row())->Value = value.toString();
              else if (index.column() == 2)
                  mItems.value(index.row())->Type->setCurrentIndex(value.toInt());
              else if (index.column() == 3)
                  mItems.value(index.row())->Comment = value.toString();
          
              Database *db = Database::instance();
              db->updateDoctorContact(mItems.at(index.row())->Id, mItems.at(index.row())->Value,
                                      mItems.at(index.row())->Type->currentIndex(), mItems.at(index.row())->Comment);
          
              emit columnChanged();
          
              return true;
          }
          
          void DoctorContactModel::setupModel(int id)
          {
              this->beginResetModel();
              mItems.clear();
          
              Database *db = Database::instance();
              QVector<QList<QString>> data = db->selectDoctorContact(id);
          
              for (QList<QString> row : data)
              {
                  mItems.append(new DoctorContactItem(row.at(0).toInt(), row.at(1), row.at(2).toInt(), row.at(3)));
              }
          
              this->endResetModel();
          }
          
          void DoctorContactModel::addRow(int doctorid)
          {
              int count = rowCount();
              beginInsertRows(QModelIndex(), count, count);
          
              Database *db = Database::instance();
              int id = db->insertDoctorContact(doctorid, "", 0, "");
              mItems.append(new DoctorContactItem(id, "", 0, ""));
          
              endInsertRows();
          }
          
          void DoctorContactModel::removeRow(QModelIndex &index)
          {
              beginRemoveRows(QModelIndex(), index.row(), index.row());
          
              Database *db = Database::instance();
              db->deleteDoctorContact(mItems.at(index.row())->Id);
              mItems.remove(index.row());
          
              endRemoveRows();
          }
          

          Maybe its a Problem with the Layout!? I dont know.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Aug 2017, 21:58 last edited by
            #6

            Since it's connected to a database, why not use a QSqlTableModel ?

            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 Offline
              F Offline
              Fuel
              wrote on 23 Aug 2017, 00:03 last edited by Fuel
              #7

              Maybe thats better, but i dont want to rewrite all my Code. Im fine with that Solution. Im just more interested now why the TableView renders outside its content.

              update: finally i found out that one line is causing the problem.

              void DoctorContactDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
                  if (index.column() == 0)
                  {
                      painter->fillRect(option.rect, QBrush(QColor(215, 215, 215, 84)));
                      QStyledItemDelegate::paint(painter, option, index);
                  }
                  else if (index.data().canConvert<DoctorComboBox*>())
                  {
                      DoctorComboBox *cbox = qvariant_cast<DoctorComboBox*>(index.data());
                      cbox->setGeometry(option.rect);
                      cbox->render(painter, painter->deviceTransform().map(option.rect.topLeft())); //<--- this line
                  }
                  else
                      QStyledItemDelegate::paint(painter, option, index);
              }
              
              1 Reply Last reply
              0
              • F Offline
                F Offline
                Fuel
                wrote on 23 Aug 2017, 18:12 last edited by
                #8

                Nobody has an Idea? It seems like that the TableView has generally Problems with ComboBoxes. I added a normal QComboBox to that Cell and the Problem still exists.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 23 Aug 2017, 21:35 last edited by
                  #9

                  Do you really need to do custom painting for your DoctorComboBox ? Can't it be painted like classic QComboBox ?

                  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 Offline
                    F Offline
                    Fuel
                    wrote on 23 Aug 2017, 22:31 last edited by
                    #10

                    What do you mean with that?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 24 Aug 2017, 06:41 last edited by
                      #11

                      The thing is, it seems that you want to show your custom combo box like it was a permanent editor, is that the case ?

                      If not, I was just wondering why you were rendering the widget itself. Your model could return the data for the different roles needed to show the data the way you want it (i.e. background role, decoration role etc.).

                      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 Offline
                        F Offline
                        Fuel
                        wrote on 24 Aug 2017, 09:05 last edited by
                        #12

                        It would be nice to show the Data in a ComboBox permanently. I tried to render a QLineEdit and have the same Problem as with the ComboBox. I dont know why i cant render an Element without having that Problem.

                        I want that the User can see that the Data is changeable and because of that i picked directly a ComboBox. You have an Idea how i can solve that different?

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          Fuel
                          wrote on 25 Aug 2017, 19:10 last edited by
                          #13

                          Nobody can help me? I cant render any Element in this Table without having this Problem. Is that a Bug?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 25 Aug 2017, 20:58 last edited by
                            #14

                            First, I'd check with a delegate like the Star Delegate sample to see if everything goes well with custom painting. Then I'd check with just a rendering a combo box without any modification to the painter.

                            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 Offline
                              F Offline
                              Fuel
                              wrote on 28 Aug 2017, 02:57 last edited by Fuel
                              #15

                              I tested it now with another Widgets and the Problem is still existing. So its not a Problem with only QComboBox. So it seems its a generally Problem with rendering. I have a similar Widget like in the StarDelegate Example and the Problem occurs. I tried not to modiy the QPainter but the Problem still exists. I also deleted the QTableView and created a new One.

                              Any other Ideas?

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 28 Aug 2017, 08:53 last edited by
                                #16

                                Hi
                                I newer say such issue with
                                https://wiki.qt.io/Combo_Boxes_in_Item_Views

                                I think you ask it to draw in wrong position with
                                painter->deviceTransform().map(option.rect.topLeft()))

                                You should check the y pos when its drawing wrongly.

                                Else it look ok from fast glance.

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Fuel
                                  wrote on 28 Aug 2017, 11:27 last edited by Fuel
                                  #17

                                  @mrjj said in QTableView add Rows renders wrong:

                                  painter->deviceTransform().map(option.rect.topLeft()))

                                  If i delete this part the Problem still exists. I took a look at your Link. In this Example there is no paint Event and im wondering. How does the Delegate know how to paint the Element?

                                  If i remove the render Part the ComboBox isnt shown.

                                  I think its a Problem with the Painter or the Renderfunction. Or perhaps something in my Layout.

                                  Update:

                                  It looks like its a Layout Problem. When i switch the Buttons to the Upper i dont have that Problem.
                                  0_1503921395908_Unbenannt.png

                                  But why? Its a normal Gridlayout with a normal Row under the QTableView.

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 28 Aug 2017, 20:51 last edited by
                                    #18

                                    Can you show the code that setup everything including the buttons ?

                                    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 Offline
                                      F Offline
                                      Fuel
                                      wrote on 29 Aug 2017, 07:16 last edited by
                                      #19

                                      I use the Designer. Cant show any Code, but only the XML Part which is not editable.

                                      0_1503991011571_Unbenannt.png

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        Fuel
                                        wrote on 20 Sept 2017, 21:16 last edited by
                                        #20

                                        still have the problem. the rows renders outside of the tableview.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 21 Sept 2017, 06:35 last edited by
                                          #21

                                          Then can you provide a minimal compilable example so people can test that locally to help find the problem ?

                                          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