How can I create a QTableView multiline cell?
I'm filling the table using the code bellow.
But Whem GetDescription() returns a long string, the content is terminated with ...
There is some way to automatic break the line?
QStandardItemModel * model = new QStandardItemModel(logos.size(), 2, this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Nome")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Descrição")));
int row = 0;
foreach(Item * item, items)
{
QStandardItem* check = new QStandardItem(true);
check->setCheckable(true);
model->setItem(row, 0, check);
QStandardItem *nameItem = new QStandardItem(QString(item->GetName()));
nameItem->setEditable(false);
model->setItem(row, 1, nameItem);
QStandardItem *descriptionItem = new QStandardItem(item->GetDescription());
descriptionItem->setEditable(false);
descriptionItem->setToolTip(logo->GetDescription());
model->setItem(row, 2, descriptionItem);
row++;
}
ui->tableView->setModel(model);
ui->tableView->resizeColumnToContents(0);
ui->tableView->resizeColumnToContents(1);
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
I think word wrapping is what you're looking for. Make sure that you've enabled wordwrap for the QTableView, then manually resize the rows to fit their contents. That will replace the ellipse with the text.
As you mentioned in your answer, you can set the QHeaderView to resize to contents automatically, but if you do a lot of adding and removing this will slow things down. I prefer to manually resize with a large addition/subtraction, particularly since the user might find it annoying to be unable to resize it themselves.
Here's some example code that enables word wrap, sets the ellipse to appear in the middle (my preference), and then manually resizes the row height to fit the contents at word boundaries:
ui->tableView->setWordWrap(true);
ui->tableView->setTextElideMode(Qt::ElideMiddle);
ui->tableView->resizeRowsToContents();
I only add to my code:
ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
As far as I know, the only way to implement multiline text drawing in cells is implementing own delegate.
You can derive from QItemDelegate.
You'll have to
implement own sizeHint function, based on QFontMetrics
and override drawDisplay function to actually display text. You can use QPainter::drawText to display multiline text. So, you don't have to care about drawing focus and selection rectangles and own painting function will be simple.
tableView->resizeRowsToContents();
Related
The database stores "Notes" about the company, and since there can be an unlimited number of notes, I made their output using dynamic QTextEdit and dynamic QFrame. Everything would be fine, but here's the problem - the size of QTextEdit is fixed, which makes it look rather miserable. How to adjust the size? The text of the notes cannot be changed, that is, the text will always be fixed, it comes from the database.
QSqlRecord rec = query.record();
while(query.next()){
dynamicText *text = new dynamicText(this);
text->setReadOnly(true);
text->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
text->setFont(defaultfont);
dynamicFrame *frame = new dynamicFrame(this);
frame->setStyleSheet("background-color:rgb(220, 220, 220)");
QHBoxLayout *lay = new QHBoxLayout(frame);
text->setText(query.value(rec.indexOf("text")).toString());
text->setStyleSheet("background-color: transparent");
lay->addWidget(text);
lay->addWidget(date);
ui->verticalLayout_11->addWidget(frame);
}
I tried with text->resize(), but I couldn't find the right arguments, almost always the text remains the same.
I am making an image annotation system Using qrect and Simple text items.
I am trying to store the string values from the QGraphicssimpletext items into a JSON file to save and load the annotation boxes. The rectangles work fine but I cannot understand how to get a string value. This is the foreach I am trying to loop through for each item, and as the text items are children of the rectangles the position does not matter.
foreach(QGraphicsItem* item, items())
{
arrayPosX.push_back(item->x());
arrayHeight.push_back(item->boundingRect().height());
arrayWidth.push_back(item->boundingRect().width());
arrayPosY.push_back(item->y());
arrayAnnotation.push_back(item->?);
}
Both the simple text and rect items are added to the scene using
itemToDraw = new QGraphicsRectItem;
this->addItem(itemToDraw);
simpleTextToDraw = new QGraphicsSimpleTextItem;
this->addItem(simpleTextToDraw);
I would simply like to know how I could get the string values from the simple text item as to allow saving and loading of both strings and boxes, not just boxes which the current system can do.
You have to cast and verify that the pointer is not null:
// ...
arrayPosY.push_back(item->y());
if(QGraphicsSimpleTextItem* text_item = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(item)){
arrayAnnotation.push_back(text_item->text());
}
I'm using a QTableWidget for a simple table. Now each cell should contain two numbers, but I don't want them to be left to right, but for readability on top of each other. I'm using Qt 5.8.
Hence, I tried something like this:
QTableWidget* table=new QTableWidget;
auto item = new QTableWidgetItem(QString("%1\n%2").arg(1).arg(2));
table->setItem(0, 0, item);
Interestingly, the outcome was that the newline \n was completely ignored. I obtain a cell containing 12. Replacing \n by <br> didn't helped.
Any idea, what I'm doing wrong/missing?
This should work, just try to resize the height of your row to give the item enough space.
wordI have a vector of strings and I need to assign each string to a check box. I am trying to insert the check boxes into a scroll area. I have a pre-made scroll bar on my main UI that is named scroll bar. How do I represent each string in my vector as a check box in my scroll area?
Currently I have:
for(auto word: words){ ///words is a vector of words
//My attempt to dynamicaly create a check box
QCheckBox *checkbox = new QCheckBox( QString::fromStdString(word);
this->ui->scrollArea->setWidget(checkbox);
}
For some reason this code will only add a single check box with the word to the scroll area.
PS if there is another widget that would be easier to use other then a scroll bar I can use that instead as long as I have the ability to scroll.
I would personally use a container widget as follows:
QWidget container;
QVBoxLayout* containerLayout = new QVBoxLayout();
container.setLayout(containerLayout);
ui->scrollArea->setWidget(container);
for(auto word: words){
QCheckBox *checkbox = new QCheckBox(QString::fromStdString(word));
containerLayout->addWidget(checkbox);
}
Note that your original code is syntactically incorrect. I added the missing closing bracket.
I am having trouble to put new line in QTableWidgetItem. Here is the code :
QTableWidget* item = new QTableWidgetItem;
item->setText("Line1 \n Line2");
With this code the item text is not displayed in two lines, i.e it seems that \n character is ignored.
I have a workaround to do this, with using QPlainTextEdit and the function setCellWidget, in that case everything work as expected but is more ugly, and I prefer not to make additional widget just to show text in multiple lines.
I think your code is fine to separate lines. But you should change the size of each row to show multi-lines.
And after resizing row's height:
You see it's OK.