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. Custom QTableWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Custom QTableWidgetItem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.1k Views
  • 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.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by
    #1

    0_1545295384996_2.gif

    I want the QTableWidgetItem's text' s echoMode to PassWord just like QLineEdit's function setEchoMode(Qt::PassWord);, so I customed the QTableWidgetItem( I know QLineEdit can solve my problem, but I want to use QTableWidgetItem to implement it. ).

    So, I wrote the codes just as the upper gif picture shows.

    But, now, there are two problem puzzling me at this phenomenon.

    One, why text echotext not to set the item's text?
    Two, why is there an extra widget just like QCheckBox in this item's left part?

    PS:

    /********************************************** .h */
    #ifndef WIDGET_ITEM_H
    #define WIDGET_ITEM_H
    
    #include "config.h"
    #include <QTableWidgetItem>
    #include <QVariant>
    #include <QString>
    #include <QDataStream>
    
    class WidgetItem : public QTableWidgetItem
    {
    public:
        WidgetItem();
    
        virtual QVariant data(int role) const;
        virtual void setData(int role, const QVariant &value);
    
    private:
    
        QString m_realText;
        QString m_echoText;
    };
    
    #endif // WIDGET_ITEM_H
    
    /********************************************** .cpp */
    #include "widget_item.h"
    
    WidgetItem::WidgetItem() : QTableWidgetItem()
    {
    
    }
    
    QVariant WidgetItem::data(int role) const
    {
        Q_UNUSED(role);
    
        return QVariant(m_realText);
    }
    
    void WidgetItem::setData(int role, const QVariant& value)
    {
        m_echoText = m_realText = value.toString();
    
        //m_echoText.replace(0, m_echoText.size(), QChar('.'));
        m_echoText = "echotext";
    
        QTableWidgetItem::setData(role, m_echoText);
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      You're returning m_realText for every given role - this is for sure not what you really want. Please take a look at the different roles: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
      The same goes for setData()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        You don't need a custom QTableWidgetItem. The model should not care about how items are represented.
        What you want is a custom delegate:

        class PwdDelegate : public : QStyledItemDelegate{
            Q_OBJECT
            Q_DISABLE_COPY(PwdDelegate)
        public:
            explicit PwdDelegate(QObject *parent = Q_NULLPTR) 
                : QStyledItemDelegate(parent),
                passwordChar(QApplication::style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter))
            {}
            QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{
                return QString(QStyledItemDelegate::displayText(value,locale).size(), passwordChar);
            }
        private:
            const QChar passwordChar;
        };
        

        Now you can use tableWidget->setItemDelegateForColumn(4, new PwdDelegate(tableWidget));

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        LimerL 1 Reply Last reply
        4
        • VRoninV VRonin

          You don't need a custom QTableWidgetItem. The model should not care about how items are represented.
          What you want is a custom delegate:

          class PwdDelegate : public : QStyledItemDelegate{
              Q_OBJECT
              Q_DISABLE_COPY(PwdDelegate)
          public:
              explicit PwdDelegate(QObject *parent = Q_NULLPTR) 
                  : QStyledItemDelegate(parent),
                  passwordChar(QApplication::style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter))
              {}
              QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{
                  return QString(QStyledItemDelegate::displayText(value,locale).size(), passwordChar);
              }
          private:
              const QChar passwordChar;
          };
          

          Now you can use tableWidget->setItemDelegateForColumn(4, new PwdDelegate(tableWidget));

          LimerL Offline
          LimerL Offline
          Limer
          wrote on last edited by
          #4

          @VRonin Yes, you are right. Thanks a lot..

          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