How to refuse that user select item by Mouse drag?
-
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.
-
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?
@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 apainter->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? -
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.
@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.
-
@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.
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?
-
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?
@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 apainter->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? -
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.
-
Q qazaq408 has marked this topic as solved