Differentiating between edited and selected in editable QComboBox - qt

I have an editable QComboBox. I add some items to it with associated user data.
QComboBox *myCB = new QComboBox;
myCB->setEditable(true);
myCB->addItem("Item1", "1");
myCB->addItem("Item2", "2");
myCB->addItem("Item3", "3");
When an item is selected from the combo box, I want to get its associated user data
But user types in something into the combo box, I just want to get the typed string.
if (selected_from_combobox)
return myCB->itemData(myCB->currentIndex()).toString();
else if (typed_by_user)
return myCB->currentText();
How do I differentiate between 2 cases?

You have to use 2 signals like this:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QComboBox *myCB = new QComboBox(this);
myCB->setEditable(true);
myCB->addItem("Item1", "1");
myCB->addItem("Item2", "2");
myCB->addItem("Item3", "3");
connect(myCB,SIGNAL(currentIndexChanged(int)),this,SLOT(indexChanged(int)));
connect(myCB,SIGNAL(currentTextChanged(QString)),this,SLOT(textChanged(QString)));
}

Related

How to modify window contents based on QComboBox value

I have a Qt window which contains a QComboBox and a few QLabels and QLineEdits. Based on the QComboBox value that the user chooses, I would like to dynamically change the QLabels and QLineEdits in the window while it is still open.
For example, if the QComboBox has a list of countries, and the user chooses France, I would like to change all the QLabels and QLineEdits into French; the user is then expected to fill out the QLineEdits in French before clicking on the Save/Close button at the bottom.
Can this be done in Qt?
If you are only looking for language translating, there are other ways to do that in Qt where you can use dictionaries to translate Ui text. Take a look at https://doc.qt.io/qt-5/qtlinguist-hellotr-example.html
But it sounds like your question is not meant to be only about language, so to do this you can use the QComboBox signal currentTextChanged, and a slot that will receive the current value and update labels based on that text. Alternately if you do not want to use a bunch of ifs you could use the signal currentIndexChanged and use a switch.
In my ui file I have (4) objects: a comboBox and label1 through 3.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox->addItem("Selected Option 1");
ui->comboBox->addItem("Selected Option 2");
ui->comboBox->addItem("Selected Option 3");
connect(ui->comboBox, &QComboBox::currentTextChanged,
this, &MainWindow::setLabelText);
}
void MainWindow::setLabelText(const QString comboText)
{
if(comboText == "Selected Option 1")
{
ui->label1->setText("Text when option 1 is selected");
ui->label2->setText("Text when option 1 is selected");
ui->label3->setText("Text when option 1 is selected");
}
else if(comboText == "Selected Option 2")
{
ui->label1->setText("Text when option 2 is selected");
ui->label2->setText("Text when option 2 is selected");
ui->label3->setText("Text when option 2 is selected");
}
else if(comboText == "Selected Option 3")
{
ui->label1->setText("Text when option 3 is selected");
ui->label2->setText("Text when option 3 is selected");
ui->label3->setText("Text when option 3 is selected");
}
}
In your header make sure you define setLabeText as a slot.
private slots:
void setLabelText(const QString comboText);

How to add multiple Icons into QTreeWidgetItem?

I am setting up an user manager and i need to implement User statue policy. I have a list of users in QTreeWidget and each user has its own QTreeWidgetItem. User has some atributes (ban, hasPassword...) and I want to show it instantly in list.
bool cUserManager::addUser2TreeWidget(cUser* user) {
if (!user) return false;
QTreeWidgetItem* item_user = 0;
item_user = new QTreeWidgetItem(item_AVAILABLE);
item_user->setCheckState(0,Qt::Unchecked);
item_user->setText(0, user->getName());
QWidget *userStateIcons = new QWidget();
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(new QPushButton(QIcon(PATH_ICON_BAN),""));
hLayout->addWidget(new QPushButton(QIcon("PATH_ICON_LOCK"),""));
userStateIcons->setLayout(hLayout);
ui.treeUsers->setItemWidget(item_user,1,userStateIcons);
return true;
Output:
I expect to see the:
Checkbox, Icon(if user is banned), Icon(if user has password),Icon(if user has admin rights), text (User name).
With buttons it is very hard to style it(set width, height, flat button...). Is there any proper and easier way to just insert only pixmap?
Thank you for advises and tips.

Dimension field in AX 2009 report dialog?

Under ax 2009 my requirement is to open a dialog box when I opens a report and it should show a drop down. So currently my drop down is SiteId from InventSite table. As show in code below.
public class ReportRun extends ObjectRun
{
//Dialog
DialogField dfSiteName;
//Range
InventSiteId siteName;
}
public boolean getFromDialog()
{
;
siteName = dfSiteName.value();
return true;
}
public Object dialog(Object _dialog)
{
DialogRunBase dialog;
FormDateControl siteNameControl;
;
dialog = super(_dialog);
dialog.caption("Sales Overview Range Dialog");
dialog.addGroup("Selec Range");
dfSiteName = dialog.addField(typeid(InventSiteId),"Site","Select Range");
siteNameControl = dfSiteName.control();
siteNameControl.mandatory(true);
return dialog;
}
Everything is working fine with this code. Now instead drop down of SiteId from InventSite table in dialog box I want drop down of Dimension[1] from InventSite table in dialog box. I am not able to do that. Please guide me on this.
If you code work fine and you want to add only the Dimension[1] from inventSite table go to AOT\Data Dictionary\Tables\InventSite\Field Groups\AutoLookup here you there you will see SiteId and Name fields. You need to add new field then go to properties of this new field and select in property DataField the field that you need.
If you add this new field will be visible in all lookups for InventSiteId edt.

Why does Qt list pane indicate there is a selection, even when the user does not select an item and no item appears as selected?

I do not understand why a selection is returned by my QListView, even when the user does not select an item, no item appears as selected, and clearSelection() is called on the pane before it is displayed.
Here is the relevant code that creates the list items:
class WidgetInstanceIdentifier {...} // This class is properly registered with Qt
listpane = new QListView(...);
QStandardItemModel * model = new QStandardItemModel(listpane);
int index = 0;
std::for_each(vg_list.cbegin(), vg_list.cend(), [&](WidgetInstanceIdentifier const & vg)
{
QStandardItem * item = new QStandardItem();
std::string text = "...";
item->setText(text.c_str());
item->setEditable(false);
item->setCheckable(false);
QVariant v;
v.setValue(vg);
item->setData(v);
model->setItem( index, item );
++index;
});
model->sort(0);
listpane->setModel(model);
listpane->clearSelection();
Here is a screenshot showing the dialog at the moment I click "OK". Notice that neither item in the list pane is selected:
... And here is the relevant code that tests for the selection:
QItemSelectionModel * listpane_selectionModel = listpane->selectionModel();
QModelIndex selectedIndex = listpane_selectionModel->currentIndex();
if (!selectedIndex.isValid())
{
// This block of code is not hit!!!!!!
// I expect it would be hit
}
QVariant vg_variant = listpaneModel->item(selectedIndex.row())->data();
// The following variable is properly set to correct data representing
// the first item in the list
vg_to_use = vg_variant.value<WidgetInstanceIdentifier>();
As noted in the code, the block of code that I expect to be hit in the case of "no selection" - the if (!selectedIndex.isValid()) {...} block - is not hit. Instead, the first item in the list is returned, as though it is selected. This is not desired! The user has no way to know which item is really being selected.
What am I misunderstanding? Why does Qt seem to report that there is a valid selection, even with no item selected in the list? What is the proper way to check if there is really no item selected?
I think selected item and current item are not the same things. Although, in some cases current item can be selected too, but this is not necessarily. Usually the current item indicated by the dashed outline in the view. Thus, if you want to check selection items count do it with QItemSelectionModel::selectedIndexes() function, i.e.:
QModelIndexList selected = listpane_selectionModel->selectedIndexes();
if (!selected.isEmpty()) {
// do something
}

How can I add a user editable checkbox in QTableView using only QStandardItemModel

I have a QTableView and a QStandardItemModel. Is there have a column can contain checkboxes that are user editable without using delegates or using the abstract model classes? It is not that I can't do it, I just want to minimize the code, I would find it overkill for simple check boxes.
By using model.setData(index, Qt::Unchecked,Qt::CheckStateRole) this creates the checkbox but it is not user editable (text beside checkbox is).
I used modelTX.setData(index, FALSE) but this creates a combo box containing True and False.
I'll try setItemData.
pls, check if the following example would work for you:
QStandardItemModel* tableModel = new QStandardItemModel();
// create text item
tableModel->setItem(0, 0, new QStandardItem("text item"));
// create check box item
QStandardItem* item0 = new QStandardItem(true);
item0->setCheckable(true);
item0->setCheckState(Qt::Checked);
item0->setText("some text");
tableModel->setItem(0, 1, item0);
// set model
ui->tableView->setModel(tableModel);
hope this helps, regards

Resources