Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Linking 2 QSpinBoxes

Linking 2 QSpinBoxes

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TrailWalker
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      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.

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TrailWalker
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TrailWalker
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TrailWalker
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved