I want to count QTableItemWidget which I've already selected,
Here is my code:
connect(m_table, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(onItemClicked(QTableWidgetItem *)));
int onItemClicked(QTableWidgetItem *item)
{
QString imageName;
imageName = item->data(Qt::UserRole).toString();
if (!m_editMode){
openMedia(imageName);
QTimer::singleShot(50, m_table->selectionModel(), SLOT(clear()));
}
else{
m_editBar->setTitle(QString::number(m_table->selectionModel()->selectedRows().count()));
}
}
But m_table->selectionModel()->selectedRows().count() is always 0 . any suggestion?
Selected rows is only active when all elements of a row are selected; it returns a list of all the selected rows (see isRowSelected).
Columns works the same.
Here, selected rows count is 1, row 2 is selected:
If you want to count the number of selected items in the widget (4 in the image case), you should use:
m_table->selectionModel()->selectedIndexes().count();
Use this code:
selectItems = m_table->selectedItems().count();
m_table->setSelectionMode(QTableView::MultiSelection);
MultiSelection lets you select multi selection.
Related
I am working with a qtableview that is filled by a model and has two columns. I can get the contents of a selected cell no problem but only want the contents of the second column even if the first column is clicked.
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
QString cellText;
if (index.isValid()) {
cellText = index.data().toString();
}
ui->lineEdit->setText(cellText);
}
the index looks like this for column 0:
QModelIndex(7,0,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
And for column 1:
QModelIndex(8,1,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
I tried to find a way to change the index for the cell that is clicked but I think there is no way to change it directly and I can not seem to find a way to tell my function to always use column 1 the second column.
Thanks for your time.
Code edited to reflect comment 1 below
{
QString cellText;
if (index.isValid()) {
QModelIndex idx = index;
idx = idx.sibling(idx.row(), 1);
cellText = idx.data().toString();
}
ui->lineEdit->setText(cellText);
}```
I'm not sure if the title is correct but this is the situation:
When a row is selected and contains Application Status of FULL_MEMBER,
A pushButton is enabled else it is disabled.
You have to get the text of the item (cell) in the fifth column of the selected row, each time the selection changes.
Have a slot like this in your widget:
private slots:
void tableSelectionChanged();
In the widget constructor, connect it to the table widget signal itemSelectionChanged:
connect(ui->tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(tableSelectionChanged()));
The slot definition is like this:
//retrieve a list of all selected rows
QModelIndexList list = ui->tableWidget->selectionModel()->selectedRows();
if(list.size() > 0)
{
//retrieve the index of the first (and only, maybe?) selected row
int row = list[0].row();
const int col = 4; //Application Status column id
QTableWidgetItem * item = ui->tableWidget->item(row, col); //the item we want to inspect
ui->pushButton->setEnabled( item->text() == "FULL_MEMBER" );
}
I want to create an editable QComboBox which filters results according to the search query and updates the dropdown entries accordingly.
After reading How do I Filter the PyQt QCombobox Items based on the text input? I tried to implement something similar in C++.
But I can't store anything inside the QComboBox now. Even after adding new entries through addItem() the total count remains 0.
What is the reason for this and how do I insert entries inside the QComboBox with QSortFilterProxyModel?
Here is the relevant snippet of the code:
SearchBox = new QComboBox(this);
SearchBox->setEditable(true);
// Try adding a few entries and check if they persist after changing the model
SearchBox->addItem(QString("hi"));
SearchBox->addItem(QString("bye"));
int count = SearchBox->count(); // count = 2
ProxyModel = new QSortFilterProxyModel;
ProxyModel->setSourceModel(SearchBox->model());
ProxyModel->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
SearchBox->setModel(ProxyModel);
// Check count again
count = SearchBox->count(); // count = 0 <- Why?
// Try adding new entries
SearchBox->addItem(QString("Hi again"));
count = SearchBox->count(); // count = 0 .. So new entries don't get stored
Completer = new QCompleter(ProxyModel,this);
Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
SearchBox->setCompleter(Completer);
QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), ProxyModel, SLOT(setFilterFixedString(const QString)));
QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));
Use QStringListModel to store items. Application crashes if proxy model have no items (if filter string filters out all items)(this needs further investigation - is this completer issue or combobox). This can be fixed by not applying such filter (onTextChanged(QString text) slot). Completer completes input if theres only one item (not sure if it's ok). And sometimes checkbox doubles all items (don't know why). If this issues is critical, I think you need to write custom ComboBox from scratch and this is serious work.
{
SearchBox = new QComboBox(this);
SearchBox->setEditable(true);
QStringList Items;
Items << "hi" << "bye";
StringListModel = new QStringListModel();
StringListModel->setStringList(Items);
ProxyModel = new QSortFilterProxyModel;
ProxyModel->setSourceModel(StringListModel);
ProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
SearchBox->setModel(ProxyModel);
// Check count again
int count = SearchBox->count(); // count = 2
// Try adding new entries
QStringList Items_ = StringListModel->stringList();
Items_ << "hi again";
StringListModel->setStringList(Items_);
count = SearchBox->count(); // count = 3
Completer = new QCompleter(ProxyModel,this);
Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
SearchBox->setCompleter(Completer);
QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), this, SLOT(onTextChanged(QString)));
QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));
}
void MainWindow::onTextChanged(QString Text) {
QStringList Items = StringListModel->stringList();
QString Item;
foreach(Item,Items) {
if (Item.indexOf(Text) > -1) {
ProxyModel->setFilterFixedString(Text);
return;
}
}
}
I have a table where user can select multiple rows, however I need to know indexes of top and last selected row, I tried playing with http://qt-project.org/doc/qt-5/QModelIndex.html so far I have this:
QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);
But I have no idea how can I use QItemSelectionModel to reach the item of table. How can I do that? There is no function in TableWidget that returns item based on QModelIndex, only QPoint
In order to get the first and last item in the selection range you can simply sort that list. For example:
QItemSelectionModel *selections = this->ui->tableWidget->selectionModel();
QModelIndexList selected = selections->selectedRows(3);
qSort(selected);
QModelIndex first = selected.first();
QModelIndex last = selected.last();
And now let's get the first and last table items:
QTableWidgetItem *firstItem = this->ui->tableWidget->item(first.row(), first.column());
QTableWidgetItem *lastItem = this->ui->tableWidget->item(last.row(), last.column());
Is QTableWidget::item(int row, int column) together with QModelIndex::column () and QModelIndex::row (), respectively, of any help?
I have a QTableWidget, which displays files.
What I want to do is be able to select 1 or multiple rows from this table and pass the first column contents of each row into a function to be able to manipulate.
QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
row = index.row();
qDebug() << row;
}
I've got this code but this passes the indexes in and I need the contents of the first column of the QTableWidget on the row or rows I select.
Thanks for any help in advance!
To get the content of a cell you need to use QModelIndex::data method:
QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes( );
foreach (QModelIndex index, indexList)
{
qDebug() << index->data( Qt::DisplayRole );
}
You can retrieve more information about selected cells just changing the role. Custom models can accept custom roles.