TreeView and Signals
-
wrote on 11 Oct 2022, 14:20 last edited by Nevez 10 Nov 2022, 14:22
Hello there,
First question:
I have a treeview where I export the db table as a model.
I want to place a button in each cell of this view. How can I do this most conveniently?Second question:
How can I access the text information in that cell when I click on any cell of the view that has a particular tree model?
Not: I tried:connect(view,&QTreeView::clicked,this,this,[=] (QModelIndex mindex) { qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString(); });
This code does not give the correct text information of the cell I clicked on.
because view has a tree structure like that photo.
How can I get the correct index? -
Hello there,
First question:
I have a treeview where I export the db table as a model.
I want to place a button in each cell of this view. How can I do this most conveniently?Second question:
How can I access the text information in that cell when I click on any cell of the view that has a particular tree model?
Not: I tried:connect(view,&QTreeView::clicked,this,this,[=] (QModelIndex mindex) { qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString(); });
This code does not give the correct text information of the cell I clicked on.
because view has a tree structure like that photo.
How can I get the correct index?wrote on 11 Oct 2022, 14:51 last edited by@Nevez said in TreeView and Signals:
qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();
- You could try the shorter
qDebug() << mindex.data(Qt::DisplayRole).toString();
, though I think it will give you the same. - If
mindex
is a child it should have aparent
set in it --- check that? - Are you clicking on the exact cell you want, or only somewhere on the row?
- You could try the shorter
-
@Nevez said in TreeView and Signals:
qDebug()<<treeView->model()->data(mindex,Qt::DisplayRole).toString();
- You could try the shorter
qDebug() << mindex.data(Qt::DisplayRole).toString();
, though I think it will give you the same. - If
mindex
is a child it should have aparent
set in it --- check that? - Are you clicking on the exact cell you want, or only somewhere on the row?
wrote on 11 Oct 2022, 14:55 last edited by@JonB said in TreeView and Signals:
You could try the shorter qDebug() << mindex.data(Qt::DisplayRole).toString();, though I think it will give you the same.
haha this works very interestingly correctly. Thanks.
can you look at my second question?
- You could try the shorter
-
Hi,
To customize your cells: QStyledItemDelegate
-
wrote on 12 Oct 2022, 07:10 last edited by
I applied a styleditemdelegeta as you said. Buttons were added, but this time the data in the cells were lost. I'm very new to itemdelegate.
as in the photo;
So why ? How can I keep both the button and the data side by side?
I want it to be like in this photo;
krnItemDelegate::krnItemDelegate(QObject *parent) : QStyledItemDelegate{parent} { } void krnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QPushButton button("+"); button.setGeometry(0,0,20,20); painter->save(); painter->translate(option.rect.topLeft()); button.render(painter); painter->restore(); }
-
I applied a styleditemdelegeta as you said. Buttons were added, but this time the data in the cells were lost. I'm very new to itemdelegate.
as in the photo;
So why ? How can I keep both the button and the data side by side?
I want it to be like in this photo;
krnItemDelegate::krnItemDelegate(QObject *parent) : QStyledItemDelegate{parent} { } void krnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QPushButton button("+"); button.setGeometry(0,0,20,20); painter->save(); painter->translate(option.rect.topLeft()); button.render(painter); painter->restore(); }
wrote on 12 Oct 2022, 08:07 last edited by JonB 10 Dec 2022, 08:07@Nevez
Yourpaint()
override paints yourQPushButton
, but that is all it does. So now you get that but nothing of the original you are wanting to add the button to. You need to call the base implementation either before or after you add your button. Whether that will work or will place one overlapping the other I do not know, but that is where to start from. -
wrote on 12 Oct 2022, 08:26 last edited by
I'm not sure I understand what I should do.
I couldn't find many examples on the net about what I want. Can you share sample code? -
I'm not sure I understand what I should do.
I couldn't find many examples on the net about what I want. Can you share sample code? -
wrote on 12 Oct 2022, 13:39 last edited by
it worked but overlapped each other.
I'm thinking of trying a different method (qstyleditemdelegate instead).
What are the ways to add a button instead of treeview's openable arrow icons, or to place a (+) icon instead of the arrow icon and assign a signal to the icon?
sry for my english
1/9