Can't use initial values from a connect()



  • Hello,

    I have declared a function y_slice_extract(int value). I am using this function in a mouse event

    @void MainWindow::mousejustpressed(int x,int y)
    {
    y_slice_extract(k1);
    }@

    and get the value properly and give me the right results. But when I use the same function using a press button event like this in the constructor of my widget:

    @QObject::connect(ui->pushButton_slice, SIGNAL(clicked()), this, SLOT(y_slice_extract(int 30 )));@

    I don't get inside function and cannot get my results.Do you know whythis is happening?



  • Remove this "30" from: @SLOT(y_slice_extract(int 30 ))@

    There is an example in the "QObject docs":http://doc.trolltech.com/4.7/qobject.html#connect about it

    [quote]
    Note that the signal and slots parameters must not contain any variable names, only the type. E.g. the following would not work and return false:

    @// WRONG
    QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
    label, SLOT(setNum(int value)));@
    [/quote]


  • Moderators

    Does your connect produce an error message?
    You should check the return value of connect. The parameter lists are different for signal and slot.


  • Moderators

    When you use signal-slots you need the same parameters in the signal at minimum as in the slot. (you can have more in the signal than in the slot)
    otherwise the connect functionality can't give the values to the slot because there aren't any.

    in your case you need something like :
    @connect(ui->pushButton_slice, SIGNAL(mySignal(int)), this, SLOT(y_slice_extract(int ))); @

    in your mousejustpressed function you could add a signal mySignal(int) that gets its value from the position where you clicked.
    @int value = ...;
    emit mySignal(value)@



  • anselmolsm is right: you can not use values in a connect statement.
    If you need to connect a signal providing no value to a slot that does require one, you can considder using a QSignalMapper instance.



  • I have tried this of Everall. And I made this, but still couldn't get my result. Is it necessery to add mySignal() function as I have get my mousePressEvent() function? Could somebody help me?

    @Canvas::Canvas(): QGraphicsScene(0,0,214,256)
    {
    }

    void Canvas::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    {
    emit mousejustpressed(mouseEvent->scenePos().x(),mouseEvent->scenePos().y());
    emit mySignal(mouseEvent->scenePos().y());
    }@

    @class Canvas : public QGraphicsScene
    {
    Q_OBJECT

    public:
    Canvas();

    void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent );
    

    signals:
    void mousejustpressed(int x,int y);
    void mySignal(int y);
    };@

    @namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

     Canvas *scene;
    

    Canvas *scene_y;
    Canvas *scene_x;

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    public slots:
    void mousejustpressed(int x,int y);
    void mySignal(int y);
    

    public slots:
    void push_button_File();
    void z_slice_extract();
    void y_slice_extract( int value);
    void x_slice_extract();

    };@
    @
    void MainWindow::mousejustpressed(int x,int y)
    {
    int k1=size_y-y;

    }

    void MainWindow::mySignal(int y)
    {
    int k1=size_y-y;
    y_slice_extract(k1);
    }@



  • can you please show us the code for the connect too?


  • Moderators

    i suggested :

    bq. in your mousejustpressed function you could add a signal mySignal(int) that gets its value from the position where you clicked.
    ?
    @12 int value = ...;
    emit mySignal(value) @

    the reason why I should use this signal is because obviously you want to use the second parameter of the mousejustpressed signal.

    One of the rules in signal-slots is that you can't use the second parameter only, you also have to provide the first one.

    So an alternative could be to use 2 parameters in your connect .



  • My code for the connect is

    @connect(ui->pushButton_slice, SIGNAL(mySignal(int)), this, SLOT(y_slice_extract(int )));@


  • Moderators

    mySignal is not a signal of ui->pushButton_slice but belongs to "canvas"

    Hint : did you use CTRL + Spacebare to automatically find the possible choices?


  • Moderators

    have you read the following?
    "signals and slots":http://doc.qt.nokia.com/4.7-snapshot/signalsandslots.html

    this will make everything we tried to explain to you very clear.



  • You have right. When I change ui->pushButton_slice with scene (Canvas pointer) then I get my results when I press the mouse.Lets suppose that I have a scrollbar and I want to take the value

    for instance.Then when I press the button I don't get the results. Do you know why?

    I want to run the y_slice_extract(int value), every time the value of scrollbar is changed. Also, I want the event of pressing the mouse and run again the y_slice_extract(int value), with value the pixel value respectivily.

    How this event could be done?
    Thanks for yor link and your response


  • Moderators

    If i understand you well you want 2 different objects to emit a signal and catch it with a third objects slot.

    1. connect scrollbar with y—slice—extract as we explained in another post.
      2 ) connect pressing the mouse with y—slice—extract as we explained here

    you can have several signals connected to the same slot. You can also connect a signal to another signal and so one. All those rules are explained in the signal slots link i gave you earlier.


Log in to reply
 

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