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. Change item's color in table-view on click
Forum Updated to NodeBB v4.3 + New Features

Change item's color in table-view on click

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.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.
  • M Offline
    M Offline
    maxomato
    wrote on last edited by
    #1

    Hi

    I have table-view and I want to change color of item with help QColorDialog after click on it.

    I created subclass of QItemDelegate and overrided functions createEditor and setModelData

    @
    QWidget *ColorItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,
    const QModelIndex &) const
    {

    QColorDialog* colorDialog = new QColorDialog();
    return colorDialog;
    

    }

    void ColorItemDelegate::setModelData(QWidget *editor, QAbstractItemModel model,
    const QModelIndex &index) const
    {
    QColorDialog
    colorDialog = static_cast<QColorDialog>( editor );
    QColor color = colorDialog->selectedColor();
    if ( color.isValid() )
    model->setData(index, color, Qt::EditRole);
    }
    @

    But it doesn't work because of model-view system considers editor widget as simple widget not dialog. And color in
    @ QColor color = colorDialog->selectedColor(); @
    is always not valid.

    What are there adequate methods to solve this problem?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you could try to create a "color edit fields", which has a color field and a button (like a combo box) and open the dialog from there. It could perhaps work, esopecially, as QComboBox also works.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        On our application we have similar needs (dialog to edit an item index). We do it this way:

        • in the delegate check in createEditor(), if a dialog is needed for editing. If yes, return a null pointer (that restrains the view from editing the index); if no return the base class' implementation
        • connect signal "QAbstractItemView::doubleClicked() ":http://doc.qt.nokia.com/latest/qabstractitemview.html#doubleClicked (or some overload signal from the specialized item views) to a slot
        • in that slot, check if the dialog is needed, if yes create it, show it with exec() and set the value if it returns with "OK"

        This way you still have inline editing for text fields, and create a dialog for the complex editing.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxomato
          wrote on last edited by
          #4

          Hi!

          Volker, I implemented this idea.

          I created model's class
          @
          class XPenTableModel : public QAbstractTableModel
          {
          Q_OBJECT
          public:

          XPenTableModel( QObject* parent = NULL );
          
          QVariant headerData(int section, Qt::Orientation orientation, int role) const;
          
          int rowCount(const QModelIndex &parent = QModelIndex()) const;
          
          int columnCount(const QModelIndex &parent = QModelIndex()) const;
          
          QVariant data(const QModelIndex &index, int role) const;
          
          bool setData(const QModelIndex &index, const QVariant &value, int role);
          
          Qt::ItemFlags flags(const QModelIndex &index) const;
          

          public slots:
          void showDialogIfNeed( QModelIndex index );

          };
          @
          In view's class I call showDialogIfNeed

          @
          void Trend::Settings::XPenTableWidget::on_penTableView_doubleClicked(QModelIndex index)
          {
          model->showDialogIfNeed( index );
          }
          @

          and I implemented showDialogIfNeed and overrode function flags to disable editable of color column

          @
          void XPenTableModel::showDialogIfNeed( QModelIndex index )
          {
          if ( index.column() == PrivateData::ColorOrder )
          {
          QColor color = QColorDialog::getColor();
          }
          }

          Qt::ItemFlags flags( Qt::ItemIsEnabled )
          {
          Qt::ItemFlags flags( Qt::ItemIsEnabled );

          if ( index.column() != PrivateData::ColorOrder )
              flags |= Qt::ItemIsSelectable;
          
          
          if ( index.column() == PrivateData::NameOrder )
              flags |= Qt::ItemIsEditable;
          
          return flags;
          

          }
          @

          It works fine but function showDialogIfNeed places in Model. It is mix gui and logic.

          Are there more correct solutions without the mix?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You can handle everything in the view class or another class that has access to the view. Just show the dialog in the on_penTableView_doubleClicked() slot and set the retrieved data in the model with setData().

            http://www.catb.org/~esr/faqs/smart-questions.html

            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