Non-existing parameter during signal slots connection



  • Hello Qt Forum,

    I am designing a Qwt based plotting widget and I would like to write a customized legend widget that can display/hide the curves and change the curve color. The legend box looks like this.

    alt text

    For each item in this colored QComboBox it is set to QColor object via QCombobox::model()->setData(const QModelIndex, const QVariant, int role); method. So for each combobox value change I would like to send which QColor object is selected and connect to a void setColor(QColor &color) slot inside the curve class.

    I am a little bit confused on how to implement this signal inside my legend box widget class. Any suggestion will be appreciated here.

    Thanks!


  • Moderators

    @yc2986 See http://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged. You can connect this signal to your own slot. In that slot you get the color (http://doc.qt.io/qt-5/qcombobox.html#itemData) from the combo-box using the index provided by the signal and then you can do what ever you need to do with this color.



  • Thanks @jsulm for the information. I am digging around the signal-slots model in Qt for some time but I am still confused. Since in Qt the connected signal and slot should has the valid matching parameters.

    In this case probably I need a currentIndexChanged(int) signal and a setColor(int) slot according to your recommendation. And I am supposed to use the QComboBox::itemData(int index, int role) method in my setColor(int) slot to retrieve the color object selected. Here comes the confusing point. In order to access the QComboBox::itemData(int, int) method I need a QComboBox object.

    However what I passed from signal to slot is merely a int index variable indicating which number of item in the list I have selected. It seems to me that I need to pass a QObject from signal to slots as well in order to make things work.

    I think I have a lot of misunderstandings on how signal and slot works. Could you please point out what is wrong in my above statement? Thank you very much!


  • Qt Champions 2016

    Hi
    In a slot, you can get the widget that send the signal
    using the sender() function.
    Its is however always a QWidget QObject so you must try cast to the type
    to get full access. ( to get the right type)
    Like
    void mainwin::currentIndexChanged( int index) {
    QComboBox * cb= qobject_cast<QComboBox * > ( sender() )
    if (cb) {
    cb->itemData xxx
    }
    }

    This makes it possible to have mutiple combos send to same slot and is more generic than
    accessing via "ui->combobox1->xxxx "



  • @mrjj @jsulm

    I have figured out my problem. I have a customed PlotCurve class which is inherited from QwtPlotCurve class in the Qwt library. The parent class is not a QObject at all... which I mistakenly believed should be a QObject class from the very start. My problem is like no matter how I call the QObject::sender() method the compiler keeps throwing method not found error. Now I have the solution. Just to take an extra inheritance from the QObject and all problem solve with the solution from @jsulm

    class PlotCurve : public QwtPlotCurve, public QObject {
        Q_OBJECT
        //...
    }
    

    Thank you very much for the suggestion!

    Best regards,
    yc2986

    update:
    The above double inheritance way cannot solve the problem since QwtPlotCurve should not be inherited from QObject. What I do is write a wrapper class inherited from QWidget and store the PlotCurve pointers in a private vector. Then all problem cleared. Cheer.


Log in to reply
 

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