Add button to QTableWidget cell alongside with entry - c++

I have a QTableWidget and one column of it is for specifying file path. I want to provide user with two options, writing the path, or specifying it using QFileDialog. When I added a button as cell widget, it covered whole cell and the entry disappeared.
QPushButton *browseButton = new QPushButton("...", tableWidget);
tableWidget->setCellWidget(rowCount, 3, browseButton);
How can I insert QPushButton or QAction with QIcon alongside with entry?

Related

Changing qt5 tab names dynamically

Say I have a tabwidget in my ui file
this is how im adding tabs right now:
QPlainTextEdit *tab = new QPlaintextEdit;
int index = ui->tabWidget->addTab(tab, "changeme");
Now I'm wondering if it's possible to change the name of the tab on the go,
for example when subclassing QPLainTextEdit in a class and connecting a signal to it when the text changes then i'd like to add a little star to the tab to indicate that the file has been modified, is it even possible?
QTabWidet::setTabText does what you want.
E.g:
ui->tabWidget->setTabText(index, "new text");

Trouble embedding a QComboBox in a QTableWidget

I'm making a GUI using QT, and I'd like to have the final entry in my table be a combo box. The idea is to allow the user to chose new items to put in the table from a drop-down list.
What I seem to be having trouble with is embedding this combo box inside a table cell. I've tried this:
table_widget = new QTableWidget(1, 9, Dialog);
table_widget->setObjectName(QStringLiteral("table_widget"));
add_part_combo = new QComboBox(table_widget);
add_part_combo->setObjectName(QStringLiteral("add_part_menu"));
add_part_combo->addItem(QStringLiteral("Import New Items..."));
table_widget->setCellWidget(1, 1, add_part_combo);
If I construct the combo box with Dialog, it puts the combo box in the upper left corner of the dialog (somewhat under the table). If I instead construct it with table_widget, the combo box appears in the upper left of the table (on top of the first header cell). If I don't supply a parent widget, then it doesn't appear at all.
But in no circumstance does the widget actually appear in cell 1,1.
What am I doing wrong?
The row and column parameters passed to setCellWidget are zero-indexed. Also, you don't need to provide a parent for the QComboBox since the QTableWidget will assume ownership of it when you call setCellWidget. As such, your code should be as follows:
add_part_combo = new QComboBox;
add_part_combo->setObjectName(QStringLiteral("add_part_menu"));
add_part_combo->addItem(QStringLiteral("Import New Items..."));
// Note: Row and column 0, not 1.
table_widget->setCellWidget(0, 0, add_part_combo);

How to disable whole column selection of QTableView?

void setSelectionBehavior ( QAbstractItemView::SelectionBehavior behavior )
This function accepts one of three values: for selecting items, for selecting rows and selecting cells.
Question:
I need the case when clicking a cell, it is selected, when clicking row index, row is selected but when clicking column header the whole column is not selected. As I understand this cant be done using this function.
I need the tableview to behave exactly same as when SelectionBehavior::selectItems is set.
But when user clicks on header the column should not be selected.
I am thinking about disabling column selection from QHeaderView but can't find how?
From my application:
// get header from QTableView tableView (replace with your widget name)
QHeaderView *header = new QHeaderView(Qt::Horizontal, tableView);
#if QT_VERSION < 0x50000
// Qt 4.8.1
header->setResizeMode(QHeaderView::ResizeToContents);
#else
// Qt 5.2.0
header->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif
header->setHighlightSections(false); // this is what you want
setHighlightSections(bool) slot is valid for Qt 4 and Qt 5
EDIT:
Excuse for carelessness! This only works if you use SelectRows or SelectItems with SingleSelection. You can find proof in the sources qheaderview.cpp and qtableview.cpp, slots voidQHeaderView::mousePressEvent(QMouseEvent *e); and voidQTableViewPrivate::selectColumn(int column, bool anchor);
For SelectItems can be used this slot:
header->setClickable(false);

QTreeView and QTabWidget to display only selected items of QTableViews

Given two SQLite tables addresses and messages, what is the best way to map them to a QTreeView and QTabWidget in such a way, that if I select one row in a QTableView (which is either mapped to addresses or messages), the selected item is
opened as a new tab to display its contents, and
inserted as an item in the QTreeView to represent an "opened" item.
I've managed to open new tabs by creating two custom QWidgets, one for addresses and one for messages. On selecting a row in the QTableView, the correct (either addresses or messages) QWidget is created and fed with the SQL model and the index. The widget then creates a QDataWidgetMapper and displays the given index. AddressWidget example:
AddressWidget::AddressWidget(QSqlRelationalTableModel *model, QModelIndex &index, QWidget *parent) :
QWidget(parent)
{
// ...
// set up widget mapper
mapper = new QDataWidgetMapper(this);
mapper->setModel(this->model);
mapper->addMapping(streetEdit, this->model->fieldIndex("street"));
mapper->addMapping(houseNumberEdit, this->model->fieldIndex("houseNumber"));
mapper->addMapping(zipEdit, this->model->fieldIndex("zip"));
mapper->addMapping(cityEdit, this->model->fieldIndex("city"));
mapper->setCurrentModelIndex(index);
}
How can I extend this to the QTreeView, so that the tree displays opened items? Each tab in the QTabWidget should have a corresponding item in the QTreeView.
If there is a right way of doing this, please add it or replace my scenario entirely.

How to make a ListBox in Qt?

I want to make a listbox, similar to the Combobox, but with out a drop down, just show the whole list with a scrollbar? Looking also to be able to click on and hightlight a row to delete that item if need be.
QComboBox *lv = new QComboBox(this);
lv->setGeometry(QRect(QPoint(0,100), QSize(100, 50)));
lv->addItem("hello1");
lv->addItem("hello2");
lv->addItem("hello3");
lv->addItem("hello4");
You will need to use QListWidget and QListWidgetItem for this.