Changing parameters based on qt combo box - c++

I have a combo box called x_axis_unit in qt with two options; metres and ms. When I change the options, the combo box doesn't take note of the change and stays stuck on metres after I changed the option to ms once I exit the gui. I typed in
ui->plot_type->setCurrentIndex(1);
but that doesn't set it. So what argument is needed to set the current index to its current value? Also based on the current option I'd like to run a loop that changes another parameter. So if the current text in the combo box is metres then I set a variable called axis to 0 and if it is in ms then I set the variable to 1
// Combo box code
ui->setupUi(this);
ui->x_axis_unit->addItem("metres");
ui->x_axis_unit->addItem("ms");
So how can I set the combo box to ms, it is always on metres.

You could use an enum to store the index for the combo box.
If the enum has class scope you can then change the combo box from any function in the class with "comboBox.setCurrentIndex(enum entry)" like this:
enum comboBoxSelection
{
eMetres = 0,
eMS
};
x_axis_unit = new QComboBox(parent);
x_axis_unit->insertItem(eMetres, "Metres");
x_axis_unit->insertItem(eMS, "ms");
x_axis_unit->setCurrentIndex(eMS);

Related

C++ Windows Owner Drawn Main Menu -- itemID

I am attempting to create an owner-drawn main menu, in Windows. I understand setting:
menuiteminfo.ftype = MFT_OWNERDRAW
I also know about handling the WM_MEASUREITEM and WM_DRAWITEM messages.
However, how do I know which main menu item is sending the message? (so that I can fill-in the appropriate box size and text) "itemID" seems to be the only unique identifier. But, how can I associate this pointer/handle to the item in question? I can use "lParam" to determine it is a menu item. But, I can't determine which menu item. "GetMenuItemID" is useless, as it returns "-1" for all main-menu items.
Or, am I going about this all-wrong? I have been searching for answers, for weeks. Really, all I want to do is change the text color of the main menu, from black, to white or light gray, so I can use a dark background.
The itemID field of the MEASUREITEMSTRUCT and DRAWITEMSTRUCT structs tells you exactly which menu item is being measured/drawn. This is the ID that you specify when you create/modify a menu item. That ID is specified via either:
the uIDNewItem parameter of AppendMenu(), InsertMenu(), or ModifyMenu().
the item parameter of InsertMenuItem() or SetMenuItemInfo()
the wID field of the MENUITEMINFO struct that you pass to InsertMenuItem() or SetMenuItemInfo().
Use whatever IDs you want, as long as they are unique for your menus.
You can also use the itemData field of MEASUREITEMSTRUCT and DRAWITEMSTRUCT to receive any custom data you want for owner-drawn menu items, if you so desire (like, for example, a pointer to a buffer that contains a menu item's text string). This custom value can be anything you want that is meaningful for you. You set this value in the dwItemData field of the MENUITEMINFO struct that you pass to InsertMenuItem() or SetMenuItemInfo().
This is all covered in the documentation:
Using Menus: Creating Owner Drawn Menu Items
Thank you Remy. Through the items that you mentioned, and researching the documentation for each, I was able to find a buried secret. For a Main Menu item, the "itemID" in both the MEASUREITEMSTRUCT and DRAWITEMSTRUCT is the handle to the drop-down menu of that item. From that, I added this line of code to WM_CREATE, to associate the itemID with the numerical (zero-based) position:
mItemID[i] = int(GetSubMenu(hMenu, i));
'i' is the numerical position, from left to right. I can then use a comparison statement like this, in WM_MEASUREITEM and WM_DRAWITEM:
lpmis=(LPMEASUREITEMSTRUCT)lParam; if(lpmis->itemID==mItemID[i])

Can I use the value(numeric) in the edit box to set it as the position on a scroll bar? If yes, then how?

Suppose in a dialog based application, I have a scroll bar and an edit box to display the position of the scroll bar.
Now, I want to improvise and put a value in the edit box myself and as soon as I type it, the scroll bar scrolls to that position.
you can select edit control and go to property window and make Number as TRUE.
By this user can only enter numeric value in editbox.
Use SetLimitText(); function to limit the length of character in edit control.
m_edit.SetLimitText(2); // this will accept value from 0 to 99
Call ON_EN_UPDATE this function will call every time when edit Control value changes.
Inside this function you can get the value of edit control.
CString strTxt;
m_edit.GetWindowText(strTxt);
int nIndex = _ttoi(strTxt); // you will get integer value here.
Use this integer value to set the position of scroll bar.

Trouble embedding a QComboBox in a QTableWidget

I'm making a GUI using QT, and I'd like to have the final entry in my table be a combo box. The idea is to allow the user to chose new items to put in the table from a drop-down list.
What I seem to be having trouble with is embedding this combo box inside a table cell. I've tried this:
table_widget = new QTableWidget(1, 9, Dialog);
table_widget->setObjectName(QStringLiteral("table_widget"));
add_part_combo = new QComboBox(table_widget);
add_part_combo->setObjectName(QStringLiteral("add_part_menu"));
add_part_combo->addItem(QStringLiteral("Import New Items..."));
table_widget->setCellWidget(1, 1, add_part_combo);
If I construct the combo box with Dialog, it puts the combo box in the upper left corner of the dialog (somewhat under the table). If I instead construct it with table_widget, the combo box appears in the upper left of the table (on top of the first header cell). If I don't supply a parent widget, then it doesn't appear at all.
But in no circumstance does the widget actually appear in cell 1,1.
What am I doing wrong?
The row and column parameters passed to setCellWidget are zero-indexed. Also, you don't need to provide a parent for the QComboBox since the QTableWidget will assume ownership of it when you call setCellWidget. As such, your code should be as follows:
add_part_combo = new QComboBox;
add_part_combo->setObjectName(QStringLiteral("add_part_menu"));
add_part_combo->addItem(QStringLiteral("Import New Items..."));
// Note: Row and column 0, not 1.
table_widget->setCellWidget(0, 0, add_part_combo);

Update MFC's CSliderCtrl

I am working on a C++ MFC project and bumping in the following. I have a CSliderCtrl on my form which I call MFC_scKINECTANGLE. To make it the way I want it the next piece of code is used:
MFC_scKINECTANGLE = (CSliderCtrl * ) GetDlgItem(SC_kinectAngle);
MFC_scKINECTANGLE->SetRangeMax(27);
MFC_scKINECTANGLE->SetRangeMin(-27);
MFC_scKINECTANGLE->SetPos(0);
The problem is that the slider at the start of the program is at the top of the bar whereas it should be in the middle, and when you try to grab it, it suddenly jumps to the correct position and works fine after that. How can I make sure the slider is in the middle of the bar at the start of my program?
According to MSDN CSliderCtrl::SetRangeMax (CSliderCtrl::SetRangeMin is similar):
void SetRangeMax(
int nMax,
BOOL bRedraw = FALSE
);
You need to set bRedraw parameter to TRUE to update slider.
Another (and probably better) variant - force redraw the slider after setup.
But due to bug (or feature?) in MS trackbar implementation you cannot just call CWnd::Invalidate (for deferred redraw) or even CWnd::RedrawWindow (for immediate redraw). This will have no effect.
Fortunately there are several events that force trackbar to repaint, e.g. enabling/disabling the window:
const BOOL isEnabled = MFC_scKINECTANGLE->IsWindowEnabled();
MFC_scKINECTANGLE->EnableWindow(!isEnabled);
MFC_scKINECTANGLE->EnableWindow(isEnabled);
See this discussion for details.
I was setting the range (0 - 100) and position (50) in the dialog's constructor. The slider kept showing up initially at position 0 instead. If I called GetPos() right after SetPos() it was returning 0 instead of 50.
What made it work for me was overriding OnInitDialog() and setting the range & position there instead of in the constructor.
BOOL CVolumeDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_VSliderBarNormal.SetRange(0, 100, TRUE);
m_VSliderBarNormal.SetPos(50);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

Validation in QSpinBox

I have QSpinBox which should take only odd numbers, so I've set initial value to 3 and step to 2.
QSpinBox* spinBox = new QSpinBox;
spinBox->setValue(3);
spinBox->setSingleStep(2);
When I'm using spin box arrows to modify value everything is ok. But when I input value from keyboard it can take not odd numbers to.
So is it possible to set validation which fulfills my requirements without inheriting QSpinBox and redefining its validate method?
My current solution is checking in slot if the value is odd:
void MyWidget::slotSetSpinBoxValue(int value)
{
if(value%2 != 0)
{
//call function which takes only odd values
}
else
{
//here I want to show some kind off message that value can only be odd
//call function with --value parameter
}
}
Second question is how to show some tip for QSpinBox? I would like to show tip like tool tip is shown with message that QSpinBox value should be odd. I've found statusTip property in QWidget but cant find example how to use it.
You can connect to the editingFinished signal and fix it:
void Obj::onSpinEditFinished()
{
int val = ui->spinPoints->value();
if(val % 2 == 0)
ui->spinPoints->setValue(val-1);
}
With respect to Richard's comment, I think that ctrl->setKeyboardTracking(false) would get around the checking that would otherwise happen on each keystroke, and allow the validation only to happen at the end.
I think the correct answer doesn't work perfectly. It makes you unable to input values like "12", because the value changed singal will be triggered when "1" was input and it will be corrected to "0" as 1 is an odd numer.
The fix could be using a timer to correct the values in the spinbox. E.g. we restart a timer with (500ms) timout once we received the valueChanged signal(The timer will only triggered once if you type quickly enough). And we check and correct the input in the timers timeout slot.
In python I solved this as follows using the editingFinished property of the spinbox. This only fires a signal when you hit enter or move the focus away from the box. In python I had to pass the function references or the value passed is not the updated one but the one at initialization.:
class SpinboxExample:
def __init__(self)
spinbox = QSpinBox()
spinbox.editingFinished.connect(
lambda value=spinbox.value, set_spinbox_val=spinbox.setValue:
self.value_changed_spinbox(value,set_spinbox_val, set_slider_val)
)
I then had a callback function which checked whether number was odd and if not altered the value in the spinbox to make it odd.
def value_changed_spinbox(self, get_value,set_spinbox_value):
value=get_value()
if value % 2 == 0:
value += 1
set_spinbox_value(value)
Hope that helps.
Well you can make a workaround using the valueChanged() slot:
void MainWindow::on_spinBox_valueChanged(int arg1)
{
if( arg1 % 2 == 0)
{
//for even values, show a message
QMessageBox b;
b.setText("Only odd values allowed!");
b.exec();
//and then decrease the value to make it odd
ui.spinBox->setValue( arg1 - 1 );
}
}
Now if you want to keep the old value in case the used enters an even number, you will have to either inherit from QSpinBox, or use an event filter to catch key press events, and act before the value gets changed.
To show the message when the user hovers his/her mouse over the spinbox, you will need to set the box's toolTip, which holds the string that will be shown:
UPDATE:
If you don't want a message box, you can:
use QStatusBar. It can display messages which only last for some amount of time (that you pass it). The downside of it is that the message will appear on the bar on the bottom of the window, instead of being close to the spinbox.
Place a label under the spinbox. Set the label's text to something like "only odd values are allowed" when the user enters an invalid value, and set an empty string when the user enters a good value. You could also do this dynamically:
The user inputs a wrong value
Create a QLabel with the warning text, and set Qt::WA_DeleteOnClose flag, so the label will delete itself when closed.
Create a QTimer with singleShot, and set it to fire after a couple of seconds (when you want the message to dissapear)
Connect the timer's signal to the label's close() slot. When the timer will expire, the label will be closed, and, thanks to WA_DeleteOnClose, will be deleted.