Overloading signal possible? Or adding new signals to base classes?



  • Hello, recently i created a widget with QT designer.
    This widget contains some spinboxes as well as a child widget. Now i want the spinboxes to tell the child widget two things:
    a) that their value changed to int i and b) their own id(so the child widget knows which spinbox emitted the signal) thus i need a signal thats similar to this: void valueChanged(int,int). Of course normal spinboxes dont have such a thing so i wondered if it is possible to either change the signal or to add another signal which does the things i descriped.
    Thank you for any help :)



  • You can tell which slider sent the signal to the child widget.

    In the slot of the child widget call QObject::sender() to find out.

    Alternately look into QSignalMapper.



  • adding signals to existing Qt classes is not possible. You can derive from them to achieve that, but I also suggest to use the existing signal and QObject::sender to get the originator of the signal. Then you can connect all spinboxes to one signal



  • Here are some code snippets for what iunknwn was describing.

    Setup spinboxes
    @
    m_spinbox1 = new QSpinBox(this);
    m_spinbox2 = new QSpinBox(this);
    m_spinbox3 = new QSpinBox(this);

    connect(m_spinbox1, SIGNAL(valueChanged(int)), this, SLOT(onChanged(int)));
    connect(m_spinbox2, SIGNAL(valueChanged(int)), this, SLOT(onChanged(int)));
    connect(m_spinbox3, SIGNAL(valueChanged(int)), this, SLOT(onChanged(int)));
    @

    Use sender() to identify spinbox
    @
    void onChanged(int value)
    {
    QSpinBox spinBox = qobject_cast<QSpinBox>(sender());
    if (spinBox == m_spinBox1) ...
    else if (spinBox == m_spinBox1) ...
    else if (spinBox == m_spinBox1) ...
    }
    @



  • thank you for your fast answer, i changed my program accordingly, but now im getting this "error"(the program still compiles but the connections wont work):

    "No such slot QWidget::statChanged(int) in ./ui_mainwindow.h:211
    Object::connect: (sender name: 'Stat1Box')
    Object::connect: (receiver name: 'widget')"

    I created an extra class to draw within widget and told QT designer to use the class for the widget(that part works). But when i added Slots to said class(and connected the value change from my spinboxes with some of the slots) i got this error.



  • Check that the slots are declared properly, connected properly, and that the build ran moc on the class.



  • If you defined the slots in the ui file (grafically) you have to add them by hand to your widget :-)



  • Ok i fixed it.
    At first i had used the QT designer connector to connect the spinboxes with the child widget, but this didnt work(i dont know why, maybe i didnt tell the program properly that the child widget actually is a child).
    Then i did it manually via connect(ui->spinbox,...,ui->widget...)
    that worked.
    thank you all :)

    How do i tell QT that i created a child widget when using the desiger to create it? I just placed one within the original widget form and thought it is automatically a child, but this might not be true.



  • This is true.
    If you place widgets on other widgets in designer, they will become child widgets.
    You can check that in the code, that is created from the designer .ui file: XXX_ui


Log in to reply
 

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