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:

    @QObject::connect(widthSpinBox, SIGNAL(valueChanged(int)), NewBackdropDialog, SLOT(widthValueChanged(int)));

    QObject::connect(heightSpinBox, SIGNAL(valueChanged(int)), NewBackdropDialog, SLOT(heightValueChanged(int)));@

    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



  • What is the error? Are widthSpinBox and heightSpinBox members in MyForm class? (maybe they are not accessible there). Tell us more about what are you doing.



  • 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&#40;&#41;;
        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.



  • widthSpinBox and heightSpinBox are pointer members in MyForm. The actual Objects are in Ui_NewBackdropDialog (and MyUi_NewBDDialog by extension).



  • Please post the complete classes or put them on pastebin. From your code it is not clear what is called where. And please do post the error message as requested.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.