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.
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'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.
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();
I have a Qt "Text Edit" widget in my Gui and this widget is used to log something.
I add every line(s) this way:
QString str;
str = ...
widget.textEdit_console->append(str);
by this way the Text Edit height will increase more and more after each new line.
I want it act like a terminal in this case, I mean after some number (that I set) of lines entered, for each new line the first line of Text Edit being deleted to prevent it being too big!
should I use a counter with every new line entered and delete the first ones after counter reached it's top or there is better way that do this automatically after
widget.textEdit_console->append(str);
called ?
thank cmannett85 for your advise but for some reason I prefer 'Text Edit',
I solved my problem this way:
void mainWindow::appendLog(const QString &str)
{
LogLines++;
if (LogLines > maxLogLines)
{
QTextCursor tc = widget.textEdit_console->textCursor();
tc.movePosition(QTextCursor::Start);
tc.select(QTextCursor::LineUnderCursor);
tc.removeSelectedText(); // this remove whole first line but not that '\n'
tc.deleteChar(); // this way the first line will completely being removed
LogLines--;
}
widget.textEdit_console->append(str);
}
I still don't know is there any better more optimized way while using 'Text Edit'
One simple way is to turn the vertical scroll-bar off:
textEdit_console->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
This code move cursor to the first line and then select it until end of line, next it will delete the line:
widget.textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
widget.textEdit->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
widget.textEdit->textCursor().deleteChar();
widget.textEdit->textCursor().deleteChar();