Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Problem with slot

  1. #1
    Join Date
    Aug 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with slot

    Hi,

    I've got this slot:

    void mySlot(int flags);

    next i connect this slot with some button:

    connect(button1,SIGNAL(clicked()),this,SLOT(mySlot (1)));

    when i click the button nothing happens. Earlier i had this same slot without any arguments and everything works well. I dont know why it isnt working now.

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem with slot

    You can put only argument types (and they must match with a few exceptions) into connect statement. You cannot put any parameter values nor names.

    Further reading: Signals and Slots

    PS. You can give the "flags" parameter a default value and leave it out of the connect statement..
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problem with slot

    because signal argument has to be the same type of slot argument;
    you're trying to connect signal(void) to slot(int); you should see a "console message" that says this; I think you can't in this way.....
    For your aim, you can use QSignalMapper class; otherwise you can know the button clicked so:
    Qt Code:
    1. class::myslot() {
    2. QPushButton* button = (QPushButton*) sender();
    3. if (button == button1) ..........
    4. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  4. #4
    Join Date
    Aug 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with slot

    Thanks guys for fast replay. I've solved my problem. I wanted this slot to has argument that i could know the sender. Now this is not neded. I found this method in the documentation: sender() which returns a pointer to the object that sent the signal. So all my problems are solved. Once again thank you.

  5. #5
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    I have a slightly different situation. I have a two sliders (horizontal, vertical), which I would like to scale items in a QGraphicsView. I tried to implement this in the following way:

    public slots:
    void process(int, int);

    //this would idealy draw the items, add them to scene, and display in view without any scaling
    connect(pushButton, SIGNAL(clicked()), this, SLOT(process(1,1)));

    //this would scale horizontally only
    connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(process(int,1)));

    //this would scale vertically only
    connect(verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(process(1,int)));

    In this way, process would receive the parameter for a horizontal change in scale, without any changes in the vertical scale. And vice versa. However, as previously noted neither the button nor sliders work in this case. What is the best way to accomplish this?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    Qt Code:
    1. //this would idealy draw the items, add them to scene, and display in view without any scaling
    2. connect(pushButton, SIGNAL(clicked()), this, SLOT(process(1,1)));
    To copy to clipboard, switch view to plain text mode 
    As it was said in the post above, you can give values in a connect() statment.
    i.e you can't do this-> process(1,1).
    Also, this connect statment makes no sense, since clicked() doesn't deliver any paremeters.
    you should do:
    Qt Code:
    1. connect(pushButton, SIGNAL(clicked()), this, SLOT(process()));
    To copy to clipboard, switch view to plain text mode 
    And define process() accordingly.
    Qt Code:
    1. //this would scale horizontally only
    2. connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(process(int,1)));
    To copy to clipboard, switch view to plain text mode 
    Again the same thing:
    Qt Code:
    1. connect(horizontalSlider, SIGNAL(valueChangedRight(int)), this, SLOT(processRight(int)));
    2. connect(horizontalSlider, SIGNAL(valueChangedLeft(int)), this, SLOT(processLeft(int)));
    To copy to clipboard, switch view to plain text mode 

    I know these signals do not exist, but the connect statment is correct.
    You will have to insert some logic that analyzes if it is an incriment or decriment, by the current and previous slider values.
    so:
    Qt Code:
    1. connect(horizontalSlider, SIGNAL(valueChangedRight(int)), this, SLOT(process(int)));
    2.  
    3. void MyClass::process(int pos)
    4. {
    5. if(pos > prevPos) //we moved right
    6. else //we moved left
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    Your post is very helpful and has helped me to understand the signals and slots much better. However my problem is that by creating new slots (for example, processRight(int) and processLeft(int)) I do not have access to the items created under the original process() which are required for me to scale. Maybe I have overlooked a point that you have made that allows me to accomplish this?

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    Did you read the last part of my post? (may be not, since I edit it)
    You don't need to create new slots, make one slot that connects to the valueChangedRight(int) signal, and use logic code to see if you moved right or left.

  9. #9
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Post Re: Problem with slot

    I hope you are not getting frustrated, but I have difficulty understanding how this works. Based on what I understand I have:

    Qt Code:
    1. public slots:
    2. void process();
    3. void processRight(int);
    To copy to clipboard, switch view to plain text mode 

    and:

    Qt Code:
    1. test1::test1() {
    2. setupUi(this);
    3.  
    4. connect(pushButton, SIGNAL(clicked()), this, SLOT(process()));
    5. connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(processRight(int)));
    6. }
    7.  
    8. void test1::processRight(int pos) {
    9. }
    10.  
    11. void test1::process() {
    12. QGraphicsScene *scene = new QGraphicsScene(0,0,samples*10,-115);
    13. QGraphicsPathItem *polyItemA = scene->addPath(pathA, QPen(Qt::green), QBrush(Qt::green, Qt::NoBrush));
    14. graphicsView->setScene(scene);
    15. }
    To copy to clipboard, switch view to plain text mode 

    You are saying that I need to write code in the function processRight(int) to determine whether it increments/decrements? But I still do not understand how I will be able to scale my item, for instance (polyItemA->scale(x,y).

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    I hope you are not getting frustrated
    not at all, this forum is for people to ask questions.
    If I would get frustrated I would not answer questions here (and ask some as well)
    You are saying that I need to write code in the function processRight(int)
    No I said:
    You don't need to create new slots, make one slot that connects to the valueChangedRight(int) signal,
    So:
    Qt Code:
    1. //deifne in test1.h:
    2. // m_prevPos; //initialize at start
    3. test1::test1() {
    4. setupUi(this);
    5. connect(pushButton, SIGNAL(clicked()), this, SLOT(process()));
    6. connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(processSlider(int)));
    7. }
    8. void test1::processSlider(int pos) {
    9. if(pos<m_prevPos)
    10. {
    11. //your code for what ever happens when you move left
    12. }
    13. else {
    14. //your code for what ecer happens when you move right
    15. }
    16. m_prevPos = pos;
    17. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    Thanks for the further clarification. The only problem with the code posted above is that the function processSlider:
    Qt Code:
    1. //your code for what ever happens when you move left
    To copy to clipboard, switch view to plain text mode 
    When I move left, the code that I would enter here needs to access an item that I call in process() (polyItemA). How can I access this item from processSlider?

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    I don't see the problem...
    Just access it as you would in process()...

  13. #13
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    Quote Originally Posted by high_flyer View Post
    I don't see the problem...
    Just access it as you would in process()...
    error C2065: 'polyItemA' : undeclared identifier

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    post your code please

  15. #15
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    Qt Code:
    1. test1::test1() {
    2. setupUi(this);
    3.  
    4. connect(pushButton, SIGNAL(clicked()), this, SLOT(process()));
    5. connect(horizontalSlider, SIGNAL(valueChangedRight(int)), this, SLOT(processRight(int)));
    6. }
    7.  
    8. void test1::processRight(int pos) {
    9. if(pos>prev_pos) {
    10. polyItemA->scale(pos,1);
    11. }
    12. prev_pos = pos;
    13. }
    14.  
    15. void test1::process() {
    16.  
    17. std::string string2 = "test.scf.scf";
    18.  
    19. ReadSFF sff(string2);
    20.  
    21. //int bases = sff.getNum();
    22. //QString Q_bases = QString::number(bases);
    23. //textEdit->append("Number of Bases: " + Q_bases);
    24.  
    25. uint_4 samples = sff.returnNumSamples();
    26. //QString Q_samples = QString::number(samples);
    27. //textEdit->append("Number of Samples: " + Q_samples);
    28.  
    29. uint_2 *sA = new uint_2[samples];
    30. uint_2 *sC = new uint_2[samples];
    31. uint_2 *sG = new uint_2[samples];
    32. uint_2 *sT = new uint_2[samples];
    33. const int size = samples;
    34.  
    35. QPolygonF polygonA;
    36. QPolygonF polygonC;
    37. QPolygonF polygonG;
    38. QPolygonF polygonT;
    39.  
    40. int xScale = 10;
    41. int yScale = 10;
    42.  
    43. for (int i=0; i<size; i++) {
    44. sA[i] = sff.returnSamples_A(i);
    45. sC[i] = sff.returnSamples_C(i);
    46. sG[i] = sff.returnSamples_G(i);
    47. sT[i] = sff.returnSamples_T(i);
    48.  
    49. if ((sA[i]/yScale) > 125) {
    50. polygonA << QPointF(xScale*i,-125);
    51. } else {
    52. polygonA << QPointF(xScale*i,-(sA[i]/yScale));
    53. }
    54.  
    55. if ((sC[i]/yScale) > 125) {
    56. polygonC << QPointF(xScale*i,-125);
    57. } else {
    58. polygonC << QPointF(xScale*i,-(sC[i]/yScale));
    59. }
    60.  
    61. if ((sG[i]/yScale) > 125) {
    62. polygonG << QPointF(xScale*i,-125);
    63. } else {
    64. polygonG << QPointF(xScale*i,-(sG[i]/yScale));
    65. }
    66.  
    67. if ((sT[i]/yScale) > 125) {
    68. polygonT << QPointF(xScale*i,-125);
    69. } else {
    70. polygonT << QPointF(xScale*i,-(sT[i]/yScale));
    71. }
    72. }
    73.  
    74. QGraphicsScene *scene = new QGraphicsScene(0,0,samples*10,-115);
    75.  
    76. QPainterPath segPath;
    77. segPath.lineTo(0,-5);
    78. segPath.closeSubpath();
    79.  
    80. QPainterPath pathA;
    81. QPainterPath pathC;
    82. QPainterPath pathG;
    83. QPainterPath pathT;
    84.  
    85. pathA.addPolygon(polygonA);
    86. pathC.addPolygon(polygonC);
    87. pathG.addPolygon(polygonG);
    88. pathT.addPolygon(polygonT);
    89.  
    90. QGraphicsPathItem *polyItemA = scene->addPath(pathA, QPen(Qt::green), QBrush(Qt::green, Qt::NoBrush));
    91. QGraphicsPathItem *polyItemC = scene->addPath(pathC, QPen(Qt::blue), QBrush(Qt::green, Qt::NoBrush));
    92. QGraphicsPathItem *polyItemG = scene->addPath(pathG, QPen(Qt::black), QBrush(Qt::green, Qt::NoBrush));
    93. QGraphicsPathItem *polyItemT = scene->addPath(pathT, QPen(Qt::red), QBrush(Qt::green, Qt::NoBrush));
    94. // QGraphicsPathItem *segPath1 = scene->addPath(segPath, QPen(Qt::gray), QBrush(Qt::green, Qt::NoBrush));
    95. // QGraphicsPathItem *segPath2 = scene->addPath(segPath, QPen(Qt::gray), QBrush(Qt::green, Qt::NoBrush));
    96.  
    97. polyItemA->setPos(0,0);
    98. polyItemC->setPos(0,0);
    99. polyItemG->setPos(0,0);
    100. polyItemT->setPos(0,0);
    101. // segPath1->setPos(0,-20);
    102. // segPath2->setPos(10,-20);
    103.  
    104. //polyItemA->scale(xSlide,1);
    105.  
    106. graphicsView->setScene(scene);
    107.  
    108.  
    109. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    The compiler is right:
    Look at test1::processRight(int pos) :
    You are using polyItemA without defining it first.
    Try:
    Qt Code:
    1. void test1::processRight(int pos) {
    2. if(pos>prev_pos) {
    3. QGraphicsPathItem *polyItemA = scene->addPath(pathA, QPen(Qt::green), QBrush(Qt::green, Qt::NoBrush));
    4. polyItemA->scale(pos,1);
    5. }
    6. prev_pos = pos;
    7. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    After trying your suggestion, I get the error:
    Qt Code:
    1. .\test1.cpp(15) : error C2065: 'pathA' : undeclared identifier
    To copy to clipboard, switch view to plain text mode 

    I am wondering if there is even a way to accomplish this without getting all the data and drawing all the polygons again. Is this the best way to scale using the sliders?

  18. #18
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    Well, then define 'pathA'.
    These compiler errors are VERY clear, try thinking on your own a second before you post them here.
    You posted 2 minutes after my post with the new error, which is exactly the same as the one before.
    We help gladly, but try to think the problem on your own first!

  19. #19
    Join Date
    Feb 2007
    Posts
    32
    Thanks
    3

    Default Re: Problem with slot

    I am only showing you this, to explain to you that the polygons I am drawing are based on large data sets that must be imported and stored. I originally tried to explain that the process() function brings in all of this data, stores the data using QPolygonF, adds the polygon as a QPainterPath, and adds the path as a QGraphicsPathItem. I then explained that all the items I need to scale were stored and created under the process() function, and if you are now telling I essentially need to re-input the data in the same data structures and essentially copy all the code from process() into processRight(), then I believe there was a misunderstanding along the way. This was something that I was initially trying to avoid. But if you are telling me that this is the only solution, then okay.
    Last edited by mistertoony; 26th March 2007 at 18:00.

  20. #20
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    I originally tried to explain that the process() function brings in all of this data and draws the polygons. I then explained the items I need to scale were stored and created under the process() function,
    At least I understood it defferently.
    Your original problem was getting the signal/slots to work.
    I didn't pay any attention to what you were trying to do IN your code, I was just trying to help you get your code to work, and to explain to you the syntax rules for connecting signal and slot.
    and if you are now telling I essentially need to re-input the data in the same data structures and essentially copy all the code from process() into processRight().
    I never said anything about that.
    If at all then this (more than once):
    You don't need to create new slots, make one slot that connects to the valueChangedRight(int) signal,
    This is true, provided you can do all the work in one slot, that reacts to hte valueChangedRight(int) signal.
    So basically you can do the following:
    Qt Code:
    1. test1::test1() {
    2. setupUi(this);
    3. //connect(pushButton, SIGNAL(clicked()), this, SLOT(process()));
    4. connect(horizontalSlider, SIGNAL(valueChangedRight(int)), this, SLOT(process(int)));
    5. }
    6.  
    7. void test1::process(int pos) {
    8. //adjust your original process() code so that it will remember the last pos,
    9. //and check based on current pos values if the movement is to the left or right.
    10. {
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 26th March 2007 at 18:58. Reason: wrapped too long line

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20
  3. Replies: 16
    Last Post: 7th March 2006, 15:57
  4. Replies: 7
    Last Post: 15th February 2006, 11:34
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.