Unsolved How to create in QT main chat windows like this?
-
@qwe3
Yes all StyleSheet and Widgets use QPainter internally.
So you can make look like anything you like.https://stackoverflow.com/questions/29196610/qt-drawing-a-filled-rounded-rectangle-with-border
QPainter p(this); p.setRenderHint(QPainter::Antialiasing); QPainterPath path; path.addRoundedRect(QRectF(10, 10, 100, 50), 10, 10); QPen pen(Qt::black, 10); p.setPen(pen); p.fillPath(path, Qt::red); p.drawPath(path);
Do note the cell area is in option.rect
so you have to use its values to draw the right place. else you draw somewhere else on the view :) -
@mrjj Now I add to paint() function:
QPen pen(Qt::black, 1); painter->setPen(pen); painter->drawText(0,0,100,100,0,index.data().toString());
and it works!
But I see next 2 problems:
-
I have to write 100,100 in drawText() function. Is there any way to get size which my index.data().toString() needs?
-
When I add this lines to my paint() function I only see this text in first cell (0,0).
I have:
item = new QStandardItem("somethingABC"); item2 = new QStandardItem("somethingABC"); item3 = new QStandardItem("somethingABC"); model->setItem(0,0,item); model->setItem(1,0,item2); model->setItem(2,0,item3);
So I think I have to get 3 texts "somethingABC" in my QTableView. Right?
-
-
@qwe3 said in How to create in QT main chat windows like this?:
1
The cell area is in option.rect. (one of the parameters to paint)
2:
i think its related to 1 as all cells then paint its text in 100,100 :)painter->drawText(option.rect, index.data().toString());
-
@mrjj One line and 2 problems aren't now problems :D Perfect!
-
@qwe3
Super.
Delegates are a bit complicated to begin with but later you love them as you can do anything you like.
and then have good performance too \o/ -
@mrjj I create rounded rectangle and it works :)
But I don't know how can I do the last thing:
The message text can be longer than one line. I think I have to change row's height. This is not a problem: table->setRowHeight(1,240);
But when I have to do that? In delegate functions()?
-
@qwe3
Hi
Nope, its not the delegates job
if its for all rows thenQHeaderView *verticalHeader = myTableView->verticalHeader(); verticalHeader->setSectionResizeMode(QHeaderView::Fixed); verticalHeader->setDefaultSectionSize(24);
-
@mrjj Thank you! I have to change your code to:
verticalHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
But you helped me next time! :)
Thank you
-
@qwe3
Hi
Super.
Congratulation on your first Delegate. -
@mrjj Only in 50%.
I need one more:
QWidget *delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { return nullptr; } void delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } void delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.row()%2==0 && index.column()!=1) { QPen pen2(Qt::yellow, 1); painter->setPen(pen2); QPainterPath path; path.addRoundedRect(QRectF(option.rect), 10, 10); painter->fillPath(path, Qt::yellow); painter->drawPath(path); QPen pen(Qt::black, 1); painter->setPen(pen); painter->drawText(option.rect,Qt::AlignCenter, index.data().toString()); } }
I would like to select text message, click on it right mouse button, get the list, where is "copy" and other possibilities. How can I do this?
Now I can't select text and click right mouse button
-
Hi
For right click menu you can use a context menuhttps://forum.qt.io/topic/31233/how-to-create-a-custom-context-menu-for-qtableview/8
-
@mrjj Next small problem :)
I have now ( in the picture is small error - the third row have smaller rectangle ( whre is text "somethingABC ") - I change it in paint application; this rectangle have width like the others )
My delegate:
path.addRoundedRect(QRectF(option.rect), 10, 10);
When I change it to:
path.addRoundedRect(QRectF(option.rect.x(), option.rect.y(),option.rect.width()/2,option.rect.height()), 10, 10);
I get:
There are 3 columns in QTableView:
the first one for my messages
the second one - seperator
the third for other person's messagesI would like to align to right the third column - yellow rectangles. Like this:
With the text there is not a problem:
painter->drawText(QRect(option.rect),Qt::AlignVCenter, index.data().toString());
But function
painter->drawPath(path);
doesn't have a parameter with alignments
-
@qwe3
Hi
Only drawText uses align. Not other paint functions as far as i know :0Since option.rect IS the cell, i think you just need to set
ui->table->horizontalHeader()->setStretchLastSection(true);so the last col is actually to the far right
-
@mrjj Thank you, I think I can use something like:
path.addRoundedRect(QRectF(option.rect.x()+100, option.rect.y(),option.rect.width()/2,option.rect.height()), 10, 10);
for the last column.
But there is next problem :D
When I use:
table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
I get the perfect height for cell (0,0). Perfect. But cell (0,1) must has the same height like cell (0,0), so it can be too big for cell (0,1).
-
@qwe3
Hi- I get the perfect height for cell (0,0). Perfect. But cell (0,1) must has the same height like cell (0,0), so it can be too big for cell (0,1).
Im not really sure what you say here :)
-
Please look at first row. In cell (0,0) there is text "somethingABC\ntext\njdsa". In cell ( 0,2 ) there is text "so". When I usetable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
QT resize height of the first row to height which cell (0,0 ) needs. But cell (0,2) must have the same height. In cell (0,2) there is only text "so" , so it can have smaller height.
-
Hi
But that is what ResizeToContents does ?
So if it has lesser text, it will be smaller.Do you want all to have same size regardless of text ?
ahh you mean the "so" should be smaller ?
-
@mrjj
I would like something like that: -
@qwe3
Hi
I think that will be hard as one row can only have one height.
https://doc.qt.io/archives/qt-4.8/qtableview.html#setRowHeight
but you can span cells to give them uneven sizes
void QTableView::setSpan(int row, int column, int rowSpanCount, int columnSpanCount)But im wondering if it would not be easier to just use a QListView ( you can reuse delegate)
and have 2 of them .
one in each side and then just some white Qwidget in between ? -
@mrjj I never used QListView. I don't need to use QTableView. But when I add 2 QListView there are two sliders to move? I would like to have only one like in QTableView ( on the right )