QGridLayout set maximum column width - c++

Is it possible to somehow set the maximum width that a QGridLayout column can expand to?
If I set a maximum width to the widgets inside a column, the column keeps growing even after the widget size limit is reached.
I'm fairly new to Qt, so maybe I'm missing something... in WPF it was a piece of cake (by setting the MaxWidth property of a ColumnDefinition object).

i think that you should insert a layout that contain the widjet into the QGridLayout :
> QHboxLayout * WidgetLayout = new QHboxLayout();
> WidgetLayout->addWidget(Your Widget)
>
> YourGridLayout->addLayout(WidgetLayout , /*your widjet Positionn Goes here */) // instead of adding your Widjet to the GridLayout , add the layout that u have created using the methode addLayout
i hope it helps

Related

How to fit the size of QTextEdit to the size of the text in it?

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.

QTableWidget having multiples lines in cell

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.

Dynamcaly adding QcheckBoxes to a QScrollArea?

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.

QTableView with multiline cell

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();

How do I access a member tab of a QTabWidget class by using a variable name?

In a function I am currently working on, I am creating a multi-dimensional array of checkboxes, with the dimensions specified at run-time by the user. In order to represent the 'z' dimension, I create multiple tabs -- each one representing a different dimension -- and create an array of check boxes in each tab. The tabs are labeled dim1, dim2, dim3, ... etc.
The problem I am having is the fact that in order to create the array of check boxes (within 3 'for' loops), I have to call the tabs as follows:
checkBoxVector.append(new QCheckBox( ui->dim1 ));
Where checkBoxVector holds pointers to the checkboxes. Now my first thought was that I would simply create a variable name that would change with each loop. With each iteration, it would go: "dim1", then "dim2", "dim3", ... etc. The problem with this is that I cannot reference the tabs with a string variable, I must type in the actual name of the tab. Here is a sample of that code:
int num = k+1;
QString dim = "dim";
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
This gives me the error " 'class Ui::MainWindow' has no member named 'tab_name' ".
Therefore; my question is: how can I apply this idea of changing the NAME of the tab with each loop, without causing such an error?
EDIT: I think I forgot to mention that the tabs have already been created at this point, and have already been labeled with the "dim1", "dim2", "dim3", ... names. The only issue I am having is how to reference these tabs after they have been created. I feel like there is a simple syntax solution.
Store the pointers to tabs in an array and index it with the variable that you use for iterating over the third dimension of your multidimensional array of checkboxes, your code would look something like this:
QTabWidget* tabs[3];
tabs[0] = ui->dim1;
tabs[1] = ui->dim2;
tabs[2] = ui->dim3;
// ... and then
checkBoxVector.append(new QCheckBox( tabs[z] ));
Someone will say it better (there is a concept name for that) but you can't dynamically declare members in c++ ... if you want QString tab_name content to be in any way related to a member of ui in this part of code:
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
The compiler need to know at compile time witch member of ui it will use to find the parent of the the new QCheckBox.
Correct me if I am wrong, you have dimz tabs and dimy*dimx QcheckBox in each tabs. You don't know any dim* when you build you ui file ?
So create your own widget (QWidget) it will store an array of QcheckBox* that you can dynamically create in constructor for example. This widget will take care of the proper layout of the check boxes.
Then finally you create you own QTabWidget that will create dimz tabs of your widget. You will access your tabs with their indexes corresponding to their z coordinate.
You're trying to reference a member variable that does not exist:
int num = k+1;
QString dim = "dim";
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
The error is caused by the fact that you are passing a non-existing member variable to the constructor of QCheckBox. The object the ui pointer is pointing to has no member variable named tab_name. If I understood your description correctly, just pass the local variable tab_name to the QCheckBox constructor like this:
checkBoxVector.append(new QCheckBox( tab_name ));