I have four radio buttons, the user must choose one of the four radio buttons.
The problem is each radio button has its own name differs from the other.
How to find out which radio button chosen by the user ?
Add the buttons into a GroupBox and use findChildren, after this you can use QButtonGroup or simply iterate through all Buttons list and check name of radiobutton. It is efficient way because it works with 4 button or 1000, you should write big code if you have many buttons.
void MainWindow::on_pushButton_15_clicked(){
QButtonGroup group;
QList<QRadioButton *> allButtons = ui->groupBox->findChildren<QRadioButton *>();
qDebug() <<allButtons.size();
for(int i = 0; i < allButtons.size(); ++i)
{
group.addButton(allButtons[i],i);
}
qDebug() << group.checkedId();
qDebug() << group.checkedButton();
}
You can use the 'isChecked()' command that all qt buttons support, and check each radio button. Or, you can connect a function to the 'toggled(bool isChecked)' signal, and use that to update a value indicating which of the four radio buttons is checked.
The numeric values of the four IDs should be continuous. Given that, call GetCheckedRadioButton to determine which one is selected.
Related
I have a wxTextCtrl object and set it to be autocomplete
wxArrayString _myStringArray;
_myStringArray.push_back("abc");
_myStringArray.push_back("alpha");
_myStringArray.push_back("bnm");
_myTextCtrl->AutoComplete(_myStringArray);
I type char 'a' into it. And then a popup shown with a list of related/suggested strings (i.e "abc" and "alpha"). Now I press 'down arrow key' in order to select a string. The first time I press the button the "abc" string is selected. The second time I press the button the "alpha" string is selected.
The problem is changing the string selection by pressing UP and DOWN arrow keys doesn't change the text control value. I want the text control value to be updated when the selected string changed by pressing UP and DOWN arrow key.
I thought I could do that manually if I know the event name. So the question is: what is the event name (or event macro) of changing string selection from a popup in a wxTextCtrl by pressing UP and DOWN arrow key?
Thanks
update: I am succeded on capturing KEY DOWN event by subclassing wxTextCtrl and then add an event handler for EVT_KEY_DOWN event.
void TextCtrlChild::keyHandler(wxKeyEvent& event)
{
int _keyCode = event.GetKeyCode();
if(_keyCode == 315 || _keyCode == 317){ //if UP or DOWN arrow key is pressed
//TO DO: capture the highlighted string from the popup
}
event.Skip();
}
Now the question is how to capture the selected/highlighted string from the popup?
The way autocompletion works is dictated by the system UI conventions, so it doesn't look like a good idea to interfere with it. If you really want to have immediate selection, consider using another control, such as wxChoice, instead.
ui->xvalue->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->xvalue->setSelectionMode(QAbstractItemView::ExtendedSelection);
with these two lines I could able to select the rows in the table. But now what I need is when I click a push button "I want all the rows to be selected in the table".
void Widget::on_pushButton_2_clicked() // Select all rows push button
{
ui->xvalue->selectionModel()->???
}
I don't know how to proceed further to solve the problem.
QTableWidget inherits the selectAll function from QAbstractItemView, so it's actually quite simple. Assuming ui->xvalue is a QTableWidget, your code will look like this:
void Widget::on_pushButton_2_clicked() // Select all rows push button
{
ui->xvalue->selectAll();
}
I am using a QListView with only single selection and selection changes handled by the up and down arrows. Everything is working as expected with one minor issue. If many items are added to the top of the list then the currently selected item risks moving down and out of view. Is there any way to detect when this happens? Thanks.
I found solution, but first of all I try to do dimilar thing, I run timer and every 2 second I insert new row to the begin of list and try to detect, is selected item still visible?
//somewhere in the constructor, it is not important
ListModel = new QStandardItemModel();
ui->listView->setModel(ListModel);
Now when I want to insert new row , I wrote code, which checks is the item visible
QStandardItem* Items = new QStandardItem(QString::number(qrand()%100));
ListModel->insertRow(0,Items);
// here we get rect of current selected item
QRect rec = ui->listView->visualRect(ui->listView->currentIndex());
//here we get all listView region, convert it to rect and check is our listView
//contain selected item
if(ui->listView->viewport()->visibleRegion().boundingRect().contains(rec))
{
qDebug() << "visible";//all good
}
else
{
qDebug() << "not visible";//not good, we need do something
//you want to detect it, so you get!
//now you know that item "runs away from you"
//and you can do some actions
someAction();
}
I test it on my computer, when I can see item, I get visible word, when item tries "run away from me" I get not visible
I hope it helps.
I need a piece of code to check that the user has selected at least one row in a QTableWidget
The QTableWidget can be referenced using ui->tableWidget.
I'm trying to check there are selecting, if not, display a message box, if so, moving on to code I have written.
Thanks.
You can obtain the selected rows from the selection model like:
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
QModelIndexList *selectedRows = selectionModel->selectedRows();
if (selectedRows.size() > 0) {
// There is at lease one selected row.
}
I have a menu that contains submenus.
eg:
Item1
Item2
Item3
item A
Item B
Item3 has items under it.
At any given time 1, 2, or the items under 3 should be checked. Since I don't have an ID for Item3 I have to use the MF_BYPOSITION indicator when I try to set a check on Item3 to indicate one of its children has a checkmark. Item3 should have a checkmark if A or B are checked. I am able to check items 1 and 2 and A and B - but can't figure out item3.
I have not been able to successfully use either ::CheckMenuItem() or ModifyMenu() to set the check mark.
Can someone point me to an example that does this successfully? The docs seem to indicate it can be done, but I have been unable to do it.
EDIT
This is for a menu that is set as the menu for a dlg box. The menu bar has three items - one of which drops down to what is shown above.
Note also, it is used as a popup for a right click, but I will take any suggestions to work in either case.
I've done this before for popup menus. You will need to access the submenu by position, instead of ID. Using your example above, Item 3 would be at position 2:
CMenu popupMenu;
popupMenu.LoadMenu(IDR_MYMENU);
popupMenu.GetSubMenu(0)->CheckMenuItem(2,MF_BYPOSITION|MF_CHECKED);
.
.
.
popupMenu.GetSubMenu(0)->TrackPopupMenu(...);
However, I haven't done this with items in the menu bar.
EDIT by Tim the OP:
For completeness
To get it to work with the menu item you have to get the hmenu
// MENU_POSITION is the zero based location of the menu you want to use. (file, edit, view, help... etc)
HMENU mainMenu = ::GetMenu(m_hWnd);
HMENU subMenu = GetSubMenu( mainMenu, MENU_POSITION);
SetMenuState(subMenu);
A few moments ago I had a similar problem - a standard MFC menu bar containing at least one submenu, and the need to be able to add a check mark to the submenu parent item, when any of the submenu child items were checked.
The easiest solution (for me) turned out to be as simple as performing the update in the standard OnUpdateMenuItem(CCmdUI* pCmdUI) call. In my case I used ON_UPDATE_COMMAND_UI_RANGE() to feed a bunch of menu IDs into the same update call, but the principal is the same for a single ON_UPDATE_COMMAND_UI() map.
The code I used (edited to be more easily inserted into other people's work) is:
void CMyApp::OnUpdateMenu(CCmdUI* pCmdUI)
{
// Note, a submenu parent (which has no editable ID in the resource editor) has the SAME ID as the first child item
if (pCmdUI->m_nID == ID_FIRST_CHILD_MENU && pCmdUI->m_pSubMenu != NULL) {
// Get the child menu so we can see if any child items are checked
CMenu* pSubMenu = pCmdUI->m_pSubMenu;
BOOL fChildChecked = FALSE;
for (UINT i = 0; !fChildChecked && i < pSubMenu->GetMenuItemCount(); ++i) {
// Do something to decide if this child item should be checked...
UINT nChildID = pSubMenu->GetMenuItemID(i);
fChildChecked = IsThisChildChecked(nChildID);
}
// The POSITION of the current menu item is stored in pCmdUI->m_nIndex
CMenu* pMenu = pCmdUI->m_pMenu;
UINT flags = MF_BYPOSITION;
if (fActiveChild) flags |= MF_CHECKED;
pMenu->CheckMenuItem(pCmdUI->m_nIndex, flags);
}
// Set the enabled state of the menu item as you see fit...
pCmdUI->Enable(TRUE);
}
Et voilĂ the submenu item automagically gains a check mark when any of its child menu items has a check mark.
Hope this helps others looking for similar solutions!
John