QTreeView and QTabWidget to display only selected items of QTableViews - c++

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.

Related

How to get different items on qlistwidgets

is it possible to make it so that when selecting different rows from qtablewidget, different text is displayed in qlistwidget? data is added to qlistwidget from qcombobox, and to qcombobox from qstringlist
void MainWindow::on_addService_clicked()
{
auto item = ui->serviceBox->currentText();
ui->serviceListWidget->addItem(item);
}
I called itemSelectionChanged signal processing, but I do not know what to do?
I tried to clear the data from qlistwidget every time I selected a new line, but that's not what I wanted, when I selected a new line, there was a new text in qlistwidget
You can use the itemSelectionChanged signal of the tablewidget and connect it a slot which manipualtes the data in the listwidget.
For details:
https://doc.qt.io/qt-6/qtablewidget.html#itemSelectionChanged
https://doc.qt.io/qt-6/qtablewidget.html#selectedItems

Open a new window when a particular cell of a row in a QTableView is clicked in Qt

I have a table which displays some dynamic data. I need to implement a feature whereby if the user clicks a designated column of the row (note that this particular column doesn't need to display any data. It is simply needs to function as a place to receive this special user input (a click is what I have in mind)), I should be able to open a new window. Note that this new window DOES not need to edit the contents of the table at all.. In fact it will display some other contextual data which is not present in the table itself.
How do I implement this in Qt?
Use Signals and slots. Hope this will be helpful!
connect(tableView,SIGNAL(clicked(const QModelIndex &)),this,SLOT(function(const QModelIndex &)));
void function(const QModelIndex &index)
{
int row=index.row();
int column=index.column();
dialog->show();
//do the stuff
}

QTreeView clear selection if selected item is filtered

I have a QTreeView that displays items and a QSortFilterProxyModel for filtering. The QTreeView is configured for SingleSelection and SelectRows.
If a selected item is removed using the filter, the next non-filtered item is selected. I want to change this behaviour. My current idea is to connect to rowsAboutToBeRemoved() and clear the selection if the selected item is removed.
connect(_ui.treeView->model(), &QAbstractItemModel::rowsAboutToBeRemoved,
[this] (const QModelIndex & parent, int start, int end) {//check and clear selection});
However, QT changes the selection to the next item before the rowsAboutToBeRemoved() signal is triggered. So I cannot test if the removed item is the selected item.
Is there a better way to clear the selection if the item gets filtered? I would prefer a solution that does not involve deriving from QTreeView.
rowsAboutToBeRemoved is not a signal, it is a virtual method.
See: http://doc.qt.io/qt-4.8/qtreeview.html
It is not QTreeView, but the ItemModel that has signals for it:
http://doc.qt.io/qt-4.8/qitemselectionmodel.html#selectionChanged
The QTreeWidget has the signal on it's own:
http://doc.qt.io/qt-5/qtreewidget.html#itemSelectionChanged
But for a QTreeView one has to use the ItemModel and/or ItemDelegate.

Select item from one ComboBox and remove that item from other ComboBoxes

Im writing an QT application, where I have 3 QComboBoxes, with a list of values. I'm trying to do so, when I select one item in a QComboBox, I will remove it from the other QComboBoxes, and when I select another or nothing, it will reappear in the other QComboBoxes again.
Do you have any ideas for this?
Edit:
I have tried to use QStringList, where I had a slot, which removed it from the other QComboBoxes, but it was very bugged and often inserted 2 whitespaces and the same drink twice.
If all comboboxes contain the same items, then you can use the current index of one combobox to disable and hide that index of other comboboxes.
You can just subclass QComboBox and create a slot like this:
void MyComboBox::disableItem(int index)
{
QListView *list_view = qobject_cast<QListView*>(view());
if(list_view)
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(list_view->model());
list_view->setRowHidden(index, true);
if(model)
{
model->item(index, 0)->setEnabled(false);
}
}
}
Then you just connect the QComboBox::currentIndexChanged(int index) signal from other comboboxes to this slot. Do this for all 3 comboboxes.
You should also create a logic for enabling and showing the items again when they shouldn't be disabled. It's almost the same function as the one above. You could just create a list of indexes that should be hidden for that combobox and use that to show all the other indexes.

Add button to QTableWidget cell alongside with entry

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?