Linking 2 QSpinBoxes
Hi, I’ve used Qt Designer to create a dialog that contains 2 QSpinBoxes set for int values. One is for width and the other height. When the user changes the value in the width QSpinBox I want the height QSpinBox to immediately change it’s value (i.e. similar to adobe photoshops ‘resize image’ for using with Constrain Proportions – but with spinners). However, it’s not working. I have 2 slots:
- void widthValueChanged(int);
- void heightValueChanged(int);
These are connected from another object like this:
And this is the code for widthValueChanged(int i):
- void MyForm::widthValueChanged(int i) {
- qDebug()<<"value changed: " << i; //this line works fine
- qDebug() << widthSpinBox->value(); //this line causes the program to not compile
- heightSpinBox->setValue(i); //this line also causes the program to not compile
- }
I’m guessing that the problem has something do with there being only one valueChanged(int) signal. But I really don’t understand Signals and Slots so I’m struggling to understand what is going on. Is there a way to achieve what I am trying to do? Many thanks.
Michael
5 replies
I have problems explaining because I don’t really understand how it works. This is the code that I use for creating the dialog, it may help explain it:
- //Ui_NewBackdropDialog bdDialog;
- MyUi_NewBDDialog bdDialog; //extends Ui_NewBackdropDialog bdDialog;
- /* Ui_NewBackdropDialog is in the header file that is generated by Qt Designer. I extended it so that I can connect the Signals to Slots without needing to alter Ui_NewBackdropDialog each time it is regenerated */
- MyForm mf; //MyForm extends QDialog
- bdDialog.setupUi(&mf);
- bdDialog.widthSpinBox->setMaximum(10000);
- bdDialog.heightSpinBox->setMaximum(10000);
- bdDialog.widthSpinBox->setValue(100);
- bdDialog.heightSpinBox->setValue(100);
- mf.init(bdDialog.constrainProportionsCB, bdDialog.widthSpinBox,
- bdDialog.heightSpinBox,
- imageWidth, imageHeight);
- //mf.widthTextEdit = bdDialog.widthTextEdit;
- //mf.heightTextEdit = bdDialog.heightTextEdit;
- mf.exec();
- qDebug() << "isAccepted: " << mf.isAccepted;
The strange thing is that I also have a QCheckBox in the Dialog and I can get it to change from the void MyForm::widthValueChanged(int i) function. I can also set the values of widthSpinBox and heightSpinBox using another function in the extend dialog and by using a QPushButton. I’m sorry I can’t explain it better, this is all very fuzzy to me.
I made a mistake – it was compiling! The program was just crashing when I opened the dialog.
But I have fixed the problem now. I removed all the code for connecting the QSpinBoxes out of MyUi_NewBDDialog and into MyForm.init() (after the pointer members had been instantiated). This meant I could get rid of MyUi_NewBDDialog completely and go back to using it’s parent class Ui_NewBackdropDialog.
I still don’t understand why the old way would allow the QSpinBox to alter a member QCheckBox, and not another QSpinBox. Or why both QSpinBoxes could be altered by another QPushBox. But the codes deleted now so I can’t show you.
But I’m happy it works now! It’s so cool how code drops away when the right solution is found. I nearly always find it happens like this.
Thanks for your help guys and again I’m sorry for my hopelessly written question.
You must log in to post a reply. Not a member yet? Register here!




