Filter with QComboBox C++ - c++

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;
}
}
}

Related

QTableWidget with setItem cannot access the data in the tablewidget

void IdListForTrain::SetListIdInList(QStringList IdList)
{
QTableWidgetItem *item=nullptr;
for(int index=0;index<IdList.count();index++)
{
ui->Id_tableWidget->insertRow(index);
for(qint32 columnIndex=0;columnIndex<=1;columnIndex++)
{
item = new QTableWidgetItem;
if(columnIndex==0)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText("00:00:00");
}
if(columnIndex==1)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText(IdList.at(index));
}
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
QString tmp = item->text();
}
}
}
Hi! I am new to Qt and I'm facing a problem. I was asked to create a tablewidget with the effective date of train Ids in one column and the names of the trains in another column,I did this code to the best of my knowledge but I can't set or access the data in the widget in the line eventhough the code doesnt give any errors but it crashes everytime I open the application window at this line,
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
I'm not sure what the reason is.
Let look at first iteration - index=0 and columnIndex=0.
You go through if(columnIndex==0) condition and set item to
QTableWidget's row = 0 (index) and column = 0 (columnIndex)
Second condition skipped
You try to access item at row = 0 (index) and column = 1 with code QString tmp1 = ui->Id_tableWidget->item(index,1)->text();. But at this time you have no item assigned to that column
That's all!

Qt5 : Get value of item clicked in a listview

I'm making a Qt5.7 application where I am populating a QListView after reading stuff from a file. Here's the exact code of it.
QStringListModel *model;
model = new QStringListModel(this);
model->setStringList(stringList); //stringList has a list of strings
ui->listView->setModel(model);
ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers); //To disable editing
Now this displays the list just fine in a QListView that I have set up. What I need to do now is to get the string that has been double clicked and use that value elsewhere. How do I achieve that?
What I tried doing was to attach a listener to the QListView this way
... // the rest of the code
connect(ui->listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(fetch()));
...
And then I have the function fetch
void Window::fetch () {
qDebug() << "Something was clicked!";
QObject *s = sender();
qDebug() << s->objectName();
}
However the objectName() function returns "listView" and not the listView item or the index.
The signal already provides you with a QModelIndex which was clicked.
So you should change your slot to this:
void Window::fetch (QModelIndex index)
{
....
QModelIndex has now a column and a row property. Because a list has no columns you are interessted in the row. This is the index of the item clicked.
//get model and cast to QStringListModel
QStringListModel* listModel= qobject_cast<QStringListModel*>(ui->listView->model());
//get value at row()
QString value = listModel->stringList().at(index.row());
You should add the index as parameter of your slot. You can use that index to access the list
Your code should be some thing like this.
void Window::fetch (QModelIndex index) {
/* Do some thing you want to do*/
}

How to get the status of hardcoded QCheckBoxes in QSqlQueryModel?

I have a QTableView that shows QSqlQueryModel. The model contains checkboxes that are created in each row in the first column (which contain the ref_no; the primary-key in my db) as follow:
void MainWindow::showM(model){
ui->tableView->setModel(model);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
for( int i = 0; p<model->rowCount(); i++)
{
QCheckBox *checkBox = new QCheckBox();
ui->tableView->setIndexWidget(model->index(i,0),checkBox);
}
ui->tableView->show();
}
... and it's working fine, displaying all the information I need plus the checkboxes.
Now, I need to get the ref_no where the adjacent checkbox is checked.
How to do that ?
Use QSignalMapper (or an ad hoc solution involving a mapper using sender(), or lambdas). For instance define a member for the mapping:
QHash<QCheckBox *, int> m_mapping;
Then in your code hook it up like this:
QCheckBox *checkBox = new QCheckBox();
ui->tableView->setIndexWidget(model->index(i,0),checkBox);
m_mapping[checkBox] = i;
connect(checkBox, &QCheckBox::toggled, this, &MainWindow::onCheckBoxToggled);
Then define a slot like this:
// for the love of kittens use proper names for methods
void MyWindow::onCheckBoxToggled(bool toggled) {
QCheckBox *box = static_cast<QCheckBox *>(sender());
const int id = m_mapping.value(box);
// do something
}
Or, if you fancy lambdas, you can do the above with a capture:
connect(checkBox, &QCheckBox::toggled,
[i](bool toggled){ /* use i, toggled */ });
All of that having being said, I would strongly recommend against the idea of creating QCheckBoxes and using setIndexWidget. Instead, employ a proxy model that enriches your column by returning the Qt::ItemIsUserCheckable flag and handles reads and writes for the Qt::CheckStateRole.

C++ QListView with icons And Signals on items clicked

I have a QListview where I have set a model. The model contains the QList. Now I want to get the contents of QListview on clicking the item. But I don't know how to do this? Through some tutorials I followed I am able to get two items appear in the QListview as follows. But I dont know how to make it work?? Please anyone help. The code which I am working as follows.
listviewmodel =new QListView;
listviewmodel->setModel( createModel() );
listviewmodel->setViewMode(QListView::IconMode);
listviewmodel->setIconSize(QSize(size().width()/8, size().height()/8));
connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
QAbstractItemModel *MainWindow::createModel()
{
QStandardItemModel *model = new QStandardItemModel();
QList<QStandardItem *> listItem;
QStandardItem *item2 = new QStandardItem();
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
listItem << item2;
QStandardItem *item1 = new QStandardItem();
item1->setIcon(QIcon(QPixmap::fromImage(qimages1)));
listItem << item1;
model->appendColumn(listItem);
return model;
}
void MainWindow::ItemClicked (QModelIndex index )
{
textEdit->setText(index.data().toString());
}
I checked your code in QtCreator and it works as you described. So what is the problem? Are you getting some errors? Maybe did you not put declaration of ItemClicked(QModelIndex) below the public slots:? Are you sure that connect returns true? Try to check it:
bool success = connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
Q_ASSERT(success);
EDIT: If you want to display only icons at QListView and get some text informations after clicking on specified item, you can do it in the following way:
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
item2->setData("informations about item2", Qt::UserRole);
(...)
void MainWindow::ItemClicked (QModelIndex index )
{
QString data = index.data(Qt::UserRole).value<QString>();
ui->textEdit->setText(data);
}

QCheckbox name access

I generate checkboxes as follows:
foreach(QString filt, types){
QCheckBox *checkbox = new QCheckBox(filt, this);
checkbox->setChecked(true);
vbox->addWidget(checkbox);
}
I need to get access to these checkboxes by name but they are all called the same?
I need to read the text they display.
How can I go about this?
Is it possible to run a for loop and attach the value of i onto the end of the checkbox. So in effect, the checkbox would be called checkbox[0], checkbox [1], etc?
EDIT:
I've changed the code to the following:
for(int i=0; i<types.count(); ++i)
{
QString filt = types[i];
*checkboxCount = *checkboxCount + 1;
QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);
typecheckbox[i]->setChecked(true);
vbox->addWidget(typecheckbox[i]);
}
I thought this was a way to dynamically name the checkboxes so I can loop through them to get the text value from them.
I'm getting the error 'variable-sized object may not be initialized' on this line QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);
Any ideas to a solution/ alternate approach?
If you want to access the checkboxes later, you can just use the find children method as follows:
QStringList myStringList;
QList<QCheckBox *> list = vbox->findChildren<QCheckBox *>();
foreach (QCheckBox *checkBox, list) {
if (checkBox->isChecked())
myStringList.append(checkBox->text());
}