Data not selected when using a custom delegate in a QTableView
-
Hi everyone,
I have a problem when using a custom delegate to display data in a QTableView. If I select a row in my QTableView, the data displayed by the delegate are not selected.
See the picture below!http://s7.postimage.org/bw61agy23/table.png!
As you can see, the data in columns 3, 6, 7, and 8 at the second row are not selected.
This is the code of the delegate which renders the data in this QTableView@void RepositoryProductDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int column = index.column();if(column == RepositoryProductModel::DosageColumn || column == RepositoryProductModel::FormColumn || column == RepositoryProductModel::PackagingColumn || column == RepositoryProductModel::IcdColumn || column == RepositoryProductModel::FamilyColumn || column == RepositoryProductModel::LabColumn) { if(index.data().toString() != Px::Product::UnAvailable) QStyledItemDelegate::paint(painter, option, index); else { QTextOption center; center.setAlignment(Qt::AlignCenter); painter->save(); painter->drawText(option.rect, "-", center); painter->restore(); } } else QStyledItemDelegate::paint(painter, option, index);
}@
How can I correct it ?
Thank you.
-
Perhaps you should check, whether the cell is selected and draw the background?
-
Thank you for your reply, how can I check if a cell is selected ?
-
The cell is selected, but your delegate fails to draw the correct background. The information whether the item is selected is in the QStyleOptionViewItem argument.
@
void MyItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
bool isSelected = option.state & QStyle::State_Selected;
}
@ -
Ok, but now how can I draw the correct background ?
-
In the style option, there is a palette. This can be used for the default colors.
-
I just saw the implementation of the paint() method. You can do it much easier without it.
The item views have a TextAlignmentRole. For just centering the dash, it is sufficient to modify the data() method of your model:
@
QVariant RepositoryProductModel::data(const QModelIndex &proxyIndex, int role) const
{
if(role == Qt::DisplayRole || role == Qt::TextAlignmentRole)
int column = index.column();
bool isCenterDashColumn = column == DosageColumn ||
column == FormColumn ||
column == PackagingColumn ||
column == IcdColumn ||
column == FamilyColumn ||
column == LabColumn;
if(isCenterDashColumn && index.data().toString() != Px::Product::UnAvailable) {
// in case of empty data
// return a single dash that's displayed centered
if(role == Qt::DisplayRole)
return QString("-");
else if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
}
}
// return your regular data here...
}
@This way you do not need the delegate's paint method, and probably no home brown delegate at all.
-
I tried using the data() method before. I always had a segmentation fault with this method.
Here is the output of the debbugger
@0 RepositoryProductModel::data repositoryproductmodel.cpp 22 0x66877a66
1 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
2 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
3 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
4 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
5 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
6 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
7 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
8 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
9 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
10 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
11 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
12 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
13 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
14 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
15 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
16 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
17 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
18 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
19 QModelIndex::data qabstractitemmodel.h 398 0x6689d2a1
20 RepositoryProductModel::data repositoryproductmodel.cpp 27 0x66877ac2
... <More>
@ -
That makes a nice pattern on my screen, but nothing more.
BTW: Did I already mention that I like line 27 - that's SO beautiful! -
The line 27 is the line where index.data() is called
@if(isCenterDashColumn && index.data().toString() == Px::Product::UnAvailable) {@
index.data() calls this function in qabstractitemmodel.h
@inline QVariant QModelIndex::data(int arole) const
{ return m ? m->data(*this, arole) : QVariant(); }@which calls the data() function in my RepositoryProductModel class and the cycle begins.
-
so, you call index.data() in the model?
You like recursions, right? -
This is the second time in this thread that I feel injuried.
@Gerolf If you don't want to help, don't post and please don't be sarcastic or at least read all the thread before posting your answer. You "autoproclamed hackers" are all the same, you think you're superior to everyone, you act as you never been beginners.Yes I call index.data() in the model, yes it causes an infinite recursion, but if you didn't notice, I'm trying to solve a problem, once again if you don't want to help people like me, people that you consider "stupid", DON'T POST.
It's always the same thing with developers community, you read words like "mutual aid", "equality", but in fact it's a different story.
I see that you're a moderator, if you want you can ban me.
-
double post
-
index.data() calls model.data(). You will have to replace the former by code that does not recurse into your model.
I've copied and pasted that code from something different, so, yes it might break your code. So yes, you might get false or incomplete advice here too.
That forum is often good for giving hints into the right direction but not to produce ready to use solutions. And sometimes answers get a bit ironically too.
Giving good support advice is bidirectional communications. The one who asks, should give sufficient information to those who try to answer. Dropping a bunch of lines that basically show the same output, without some meaningful information won't count that much here.
And yes, you're supposed to use a debugger.
Including setting a breakpoint and run your program step for step.
You would have seen that recursion then.You're not supposed to copy and paste literally what you've found here and when the compiler or the runtime fails, come back with the output without further investigation. At least we both didn't have the impression that you did some investigation but went back straight. And if you did, how should we know? You didn't tell.
And if I were like you, I could lament now, it's always the same thing with developers asking in communities, dropping a bunch of error lines and expecting a full fledged solution...
BTW: The overall attitude here on DevNet is far better than on many, many other forums.
-
[quote author="shiru" date="1328187678"]This is the second time in this thread that I feel injuried.
@Gerolf If you don't want to help, don't post and please don't be sarcastic or at least read all the thread before posting your answer. You "autoproclamed hackers" are all the same, you think you're superior to everyone, you act as you never been beginners.
[/quote]Hi shiru,
I'm sorry if you fealt like I want to blaim you, that was not my intention. I just tried to give you a hint, where to look.
As you perhaps know, nearly all people here are doing this in their free time. So sometimes, you see the problem, but have no time for a long answer. What should I do? give a short hint or not answer?The technical thing is already described by Volker, so I will not go into that.