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.
Related
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 have a GTK application that has a window with a treeview and a button. When the button is clicked I need to get the data from the first (and only) column of the selected row in the treeview.
This is the class for the columns:
class ModelColumns:
public Gtk::TreeModel::ColumnRecord{
public:
ModelColumns(){ add(m_port_name); }
Gtk::TreeModelColumn<Glib::ustring> m_port_name;
};
This is like in the example here but with only one column: http://www.lugod.org/presentations/gtkmm/treeview.html
This is the button click signal handler at the moment:
tvPorts is the treeview widget
tvPortsList is the listStore for the treeview
static
void on_btnPortSelectOK_clicked (){
Glib::RefPtr<Gtk::TreeSelection> selection = tvPorts->get_selection();
Gtk::TreeModel::iterator selectedRow = selection->get_selected();
//Now what?
//Need to get data from selected row to display it.
}
I have searched the documentation and many examples to try and find out what to do next but can't find any examples for gtkmm, I can only find examples for c or python implementations.
As far as I can tell, I need to get a TreeRow object from my iterator (selectedRow) how do I do this?
Thanks.
Update:
I am now using this code and it almost works.
The only problem is that it prints the previous selection.
The first time I select something and then press the button it prints only a new line. The second time it prints what was selected the first time, the third prints the second, etc.
Glib::RefPtr<Gtk::TreeSelection> selection = tvPorts->get_selection();
Gtk::TreeModel::iterator selectedRow = selection->get_selected();
Gtk::TreeModel::Row row = *selectedRow;
Glib::ustring port = row.get_value(m_Columns.m_port_name);
printf("\nselected port: %s", port.data());
This seems odd.
(m_Columns is an instance of the ModelColumns class)
Update 2:
Fixed the problem by adding fflush(stdout);
It all works now, thanks.
The docs say to simply dereference the iter to get the TreeRow:
Gtk::TreeModel::Row row = *iter; // 'iter' being your 'selectedRow'
std::cout<<row[0];
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 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.
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