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. [SOLVED] Reg : Need some help with using custom StyledItemDelegate - Column Not Resizing
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Reg : Need some help with using custom StyledItemDelegate - Column Not Resizing

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 4.2k 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.
  • R Offline
    R Offline
    roahanramesh
    wrote on last edited by
    #1

    Hello
    I am facing the following issue. I have three SytedItemDelegates (comboboxdelegate - to display comboboxes ; datedelegate for displaying delegates and picturedelegate for displaying QPixmap). I then call setItemDelegate for column for corresponding display requirement.
    However, when the code runs the photo column does not show correctly. I need to manually click the row number for it to resize. Any pointers will be helpful.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      did you reimplement sizeHint() of your delegates correctly?

      --- 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
      • R Offline
        R Offline
        roahanramesh
        wrote on last edited by
        #3

        Thanks for the reply.

        This is the sizeHint code for

        1. comboboxdelegate

        QSize mycomboboxdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        return QSize(80,80);
        }
        void mycomboboxdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {

        painter->drawText(option.rect,Qt::AlignCenter,index.data(Qt::DisplayRole).toString());
        

        }

        1. DateDelegate

        QSize mydateeditdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        qDebug()<<"Photo Delegate Called";
        return QSize(80,80);

        }
        void mydateeditdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {

        QStyleOptionViewItemV4 display_option(option);
        display_option.text = QDate::fromString(index.data().toString(), Qt::ISODate).toString("ddd dd/MMM/yyyy");
        QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &display_option, painter);
        

        }

        1. Photo Delegate

        QSize myphotodelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        qDebug()<<"Photo Delegate Called";
        return QSize(80,80);
        }
        void myphotodelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
        QByteArray array = index.data().toByteArray();
        QPixmap pixmap;
        pixmap.loadFromData(array,"PNG",Qt::AutoColor);
        pixmap.scaled(80,80,Qt::KeepAspectRatio,Qt::FastTransformation);
        painter->drawPixmap(option.rect,pixmap);
        }

        Code where the above delegates are called.

        ui->tableView->setModel(employmentstatus);
        ui->tableView->setItemDelegateForColumn(2,photodelegate);
        ui->tableView->setItemDelegateForColumn(3,combodelegate);
        ui->tableView->setItemDelegateForColumn(5,combodelegate);
        ui->tableView->setItemDelegateForColumn(6,combodelegate);
        ui->tableView->resizeColumnsToContents();
        ui->tableView->resizeRowToContents(2);

        Please help

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          please use code-tags ... it's a pain reading code like this without 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
          • R Offline
            R Offline
            roahanramesh
            wrote on last edited by
            #5

            One Question, I am creating the above mentioned delegates and passing through the constructor the form name. Based upon form name, i am returning the QSize, Would this be a better approach when I am using the same delegates for multiple formats.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roahanramesh
              wrote on last edited by
              #6

              Hello Sorry for bothering, but can you provide me a tip on using code tags.

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                [quote author="roahanramesh" date="1377004715"]Hello Sorry for bothering, but can you provide me a tip on using code tags. [/quote]
                There is convenience button in the "toolbar" when you write/edit a post. But it's a simple as surrounding your code with @-signs.

                --- 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
                • R Offline
                  R Offline
                  roahanramesh
                  wrote on last edited by
                  #8
                  1. MyDateDelegate .cpp implementation
                    @QSize mydateeditdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    qDebug()<<"Date Delegate Called";
                    return QSize(80,80);

                  }
                  void mydateeditdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {

                  QStyleOptionViewItemV4 display_option(option);
                  display_option.text = QDate::fromString(index.data().toString(), Qt::ISODate).toString("ddd dd/MMM/yyyy");
                  QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &display_option, painter);
                  

                  }
                  @

                  1. MyCombobox Implementation
                    @
                    QSize mycomboboxdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    return QSize(80,80);
                    }
                    void mycomboboxdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {

                    painter->drawText(option.rect,Qt::AlignCenter,index.data(Qt::DisplayRole).toString());
                    }
                    @

                  2. PictureDelegate Implementation
                    @QSize myphotodelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    qDebug()<<"Photo Delegate Called";
                    return QSize(80,80);
                    }
                    void myphotodelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    QByteArray array = index.data().toByteArray();
                    QPixmap pixmap;
                    pixmap.loadFromData(array,"PNG",Qt::AutoColor);
                    pixmap.scaled(80,80,Qt::KeepAspectRatio,Qt::FastTransformation);
                    painter->drawPixmap(option.rect,pixmap);
                    }
                    @

                  3. Code where the above delegates get called and implemented
                    @ employmentstatus = new QSortFilterProxyModel(this);
                    employmentstatus->setSourceModel(branchfilter);
                    employmentstatus->setDynamicSortFilter(true);
                    employmentstatus->setFilterKeyColumn(-1);
                    employmentstatus->setFilterRegExp("ACTIVE");

                    combodelegate = new mycomboboxdelegate(formname,this);
                    datedelegate = new mydateeditdelegate(formname,this);
                    photodelegate = new myphotodelegate(formname,this);

                    ui->tableView->setModel(employmentstatus);
                    ui->tableView->setItemDelegateForColumn(2,photodelegate);
                    ui->tableView->setItemDelegateForColumn(3,combodelegate);
                    ui->tableView->setItemDelegateForColumn(5,combodelegate);
                    ui->tableView->setItemDelegateForColumn(6,combodelegate);
                    ui->tableView->resizeColumnsToContents();
                    ui->tableView->resizeRowsToContents();
                    @

                  1 Reply Last reply
                  0
                  • raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    I'm not sure if it will solve your problem:
                    but also set the delegate for the whole view.
                    @
                    ui->tableView->setItemDelegate(photodelegate);
                    @

                    And you could also combine your 2 delegates and implement them in 1 single implementation. This saves you the amount of code to manage.
                    You get also the QModelIndex passed to the methods, which lets to check in which column you currently are.

                    --- 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
                    • R Offline
                      R Offline
                      roahanramesh
                      wrote on last edited by
                      #10

                      I tried to use this approach, but however, in places where the delegates dont get applied, the lineedit does not appear, It crashes the software, could you please help.

                      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