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. How to refuse that user select item by Mouse drag?
Forum Updated to NodeBB v4.3 + New Features

How to refuse that user select item by Mouse drag?

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

    QTableView

    When user press mouse (LeftButton) int QModelIndex (0,0), then move mouse to QModelIndex (0, 5), and release mouse at this QModelIndex (0, 5).

    Now there are six items are selected, and there items's background-color are changed (white -> blue)

    How to refuse this function? In other words,when user do it ,QTableView will reset the background-color of thses item,but i had set the background-color ( by QItemDelegate ), and it will lead my set to invaild.

    JonBJ 1 Reply Last reply
    0
    • qazaq408Q qazaq408

      @JonB

      The first, thank you answer.

      But there are some exception. For instance, a column has a QItemDelegate,it likes

      void NameDelegate(QPainter* painer, const QStyleOptionVitwItem& op, const QModelIndex& modelindex)const {
          painer->stroe();
          //draw rect for some kind of important tip
          painter->fillRect(op.x()  + 2, 
                          op.y() + 2, 
                          op.width() - 4,
                          op.height() - 4,
                          QBrush(QColor(Qt::yellow);
      
          //draw name text
          QString name = index->model()->data(Qt::DisplayRole).toString();
          QRect txtRect(op.x()  + 4,op.y() + 4, op.width() - 8,op.height() - 8);
          painter->drawText(txtRect, Qt::AlignCenter, name);
      
          painter->save();
      }
                                          
      

      A column set this NameDelegate (Inherits QItemDelegate), if i use QSS,like selection-color,it will lead all background-color of this NAME column change to white,but as you see, i need this colum show some special background-color( a Rect but don't fill full item)

      So do you have any better ideas?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #4

      @qazaq408
      I am afraid I do not understand what you are saying/what you are trying to achieve.

      Meanwhile, just for the code you show what is painer->stroe() at the start, and why do you have a painter->save() at the end? Normally you save painter state at the start of your code and restore it at the end, if anything you seem to be doing the opposite?

      1 Reply Last reply
      0
      • qazaq408Q qazaq408

        QTableView

        When user press mouse (LeftButton) int QModelIndex (0,0), then move mouse to QModelIndex (0, 5), and release mouse at this QModelIndex (0, 5).

        Now there are six items are selected, and there items's background-color are changed (white -> blue)

        How to refuse this function? In other words,when user do it ,QTableView will reset the background-color of thses item,but i had set the background-color ( by QItemDelegate ), and it will lead my set to invaild.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote last edited by
        #2

        @qazaq408
        What about doing it via stylesheet, https://doc.qt.io/qt-6/stylesheet-reference.html ?

        QTableView

        The color and background of the selected item is styled using selection-color and selection-background-color respectively.

        E.g. https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qtableview

        QTableView {
            selection-background-color: ...
        }
        

        Otherwise probably need to see your code.

        qazaq408Q 1 Reply Last reply
        0
        • JonBJ JonB

          @qazaq408
          What about doing it via stylesheet, https://doc.qt.io/qt-6/stylesheet-reference.html ?

          QTableView

          The color and background of the selected item is styled using selection-color and selection-background-color respectively.

          E.g. https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qtableview

          QTableView {
              selection-background-color: ...
          }
          

          Otherwise probably need to see your code.

          qazaq408Q Offline
          qazaq408Q Offline
          qazaq408
          wrote last edited by
          #3

          @JonB

          The first, thank you answer.

          But there are some exception. For instance, a column has a QItemDelegate,it likes

          void NameDelegate(QPainter* painer, const QStyleOptionVitwItem& op, const QModelIndex& modelindex)const {
              painer->stroe();
              //draw rect for some kind of important tip
              painter->fillRect(op.x()  + 2, 
                              op.y() + 2, 
                              op.width() - 4,
                              op.height() - 4,
                              QBrush(QColor(Qt::yellow);
          
              //draw name text
              QString name = index->model()->data(Qt::DisplayRole).toString();
              QRect txtRect(op.x()  + 4,op.y() + 4, op.width() - 8,op.height() - 8);
              painter->drawText(txtRect, Qt::AlignCenter, name);
          
              painter->save();
          }
                                              
          

          A column set this NameDelegate (Inherits QItemDelegate), if i use QSS,like selection-color,it will lead all background-color of this NAME column change to white,but as you see, i need this colum show some special background-color( a Rect but don't fill full item)

          So do you have any better ideas?

          JonBJ 1 Reply Last reply
          0
          • qazaq408Q qazaq408

            @JonB

            The first, thank you answer.

            But there are some exception. For instance, a column has a QItemDelegate,it likes

            void NameDelegate(QPainter* painer, const QStyleOptionVitwItem& op, const QModelIndex& modelindex)const {
                painer->stroe();
                //draw rect for some kind of important tip
                painter->fillRect(op.x()  + 2, 
                                op.y() + 2, 
                                op.width() - 4,
                                op.height() - 4,
                                QBrush(QColor(Qt::yellow);
            
                //draw name text
                QString name = index->model()->data(Qt::DisplayRole).toString();
                QRect txtRect(op.x()  + 4,op.y() + 4, op.width() - 8,op.height() - 8);
                painter->drawText(txtRect, Qt::AlignCenter, name);
            
                painter->save();
            }
                                                
            

            A column set this NameDelegate (Inherits QItemDelegate), if i use QSS,like selection-color,it will lead all background-color of this NAME column change to white,but as you see, i need this colum show some special background-color( a Rect but don't fill full item)

            So do you have any better ideas?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by
            #4

            @qazaq408
            I am afraid I do not understand what you are saying/what you are trying to achieve.

            Meanwhile, just for the code you show what is painer->stroe() at the start, and why do you have a painter->save() at the end? Normally you save painter state at the start of your code and restore it at the end, if anything you seem to be doing the opposite?

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

              I guess you need to set the selection mode: https://doc.qt.io/qt-6/qabstractitemview.html#SelectionMode-enum

              Pick one that works for you.

              1 Reply Last reply
              0
              • qazaq408Q qazaq408 has marked this topic as solved

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved