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. Best method for attaching QWidget into a Qtable()

Best method for attaching QWidget into a Qtable()

Scheduled Pinned Locked Moved General and Desktop
8 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.
  • S Offline
    S Offline
    SimonBenAnderson
    wrote on last edited by
    #1

    Hi everyone,

    This is my first time posting here. I have been coding in Qt for a few years now, but have only recently gone a bit deeper into the Model/View/Customising Widgets.

    Im currently stuck on a problem, and was wondering if anyone could help. I have looked around but have been unable to find something that helps with regards to this problem.

    I have a QAbstractTable that gets populates about 100+ entries, To edit this table I have a QStylisedItemDelegate, that on specific cells creates a drop down with a list of users from the DB. All of this works great, I have color cells depending on which department, or heading is in a specific cell.

    Now here comes the curve ball.

    In 5 of my columns I want to populate the cell with a widget that I have writen, only when in view mode, not in edit mode, as I still want my drop-downs when editing. The widget is nothing to advanced, It draws a block for every stage under the column and gives the status of a stage a color. If the user moves over the color the tooltip gives more information about the stage.

    !https://dl.dropboxusercontent.com/u/10740198/QtQuestion.jpg(The Table and the Widget)!

    Is there a way to populate the cells with my widget, should I instead of creating instances of the my custom widget, use the Delegate.paint() function and recreate the widget inside my delegate(to me this seems a bit messy, but if it has to be done it shall) or should I look into creating a custom view. Im still new to views as so all suggestions are welcome :)

    Thank for the help everyone

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You need to implement the paint() method of delegate when you are in read only mode to display your customwidget.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SimonBenAnderson
        wrote on last edited by
        #3

        How does one display the widget in paint(), as paint I have found only allows you to paint something new, not create a widget and input it into a cell.

        Am I missing something, or a special way of creating a widget, pulling out its paint and putting it into my delegates paint?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Yes, It is painting something new. Not for the putting your existing widget. You can copy-paste the widget code as you said.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonBenAnderson
            wrote on last edited by
            #5

            Thanks Dheerendra, I was wondering if by any chance you have a simplecode snippet of something like this as im still struggling to find the correct method of doing this.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Here is snippet.

              have following enums defined for 5 columns.
              @
              enum Column {Name, Father,Family,Adhar_No,State};
              @

              So paint is called for Adhar_no column

              Tableview settings :
              @
              ui->tableView->setItemDelegateForColumn(Adhar_No,myDeligate);
              ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
              @
              Delegate Paint implementation.
              @
              void QMyDeligate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
              qDebug() << "****I am here *****"<<endl;
              painter->save();
              if (index.column() == Adhar_No){
              qDebug() << "Create Hell" << endl;
              if (option.state & QStyle::State_Selected)
              painter->fillRect(option.rect, option.palette.highlight());
              else if (option.state & QStyle::State_MouseOver)
              painter->fillRect(option.rect, option.palette.midlight());
              QFontMetrics fm(option.font);

                  QRect outline = option.rect;
                  int outmid = outline.center().y();
              
                  QRect fnBound = fm.boundingRect("filename");
                  int fnBoundMid = fnBound.center().y();
                  fnBound.moveLeft(outline.left());
                  fnBound.translate(0, outmid - fnBoundMid);
                  painter->drawEllipse(fnBound);
                  //painter->drawText(fnBound, Qt::AlignVCenter | Qt::AlignLeft, "filename");
              }
              painter->restore();
              

              }
              @

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SimonBenAnderson
                wrote on last edited by
                #7

                do you know of a way to only paint every cell on startup and then only selected cells after startup. as currently all 500cells are getting repainted ever time I select anything, and it has created a massive slow down.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonBenAnderson
                  wrote on last edited by
                  #8

                  Thanks, I finally figured it all out and its working :D hooraa!

                  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