Results 1 to 5 of 5

Thread: Emit Signal and Slot question

  1. #1
    Join Date
    Jun 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Emit Signal and Slot question

    Hi, I'm new to Qt (and GUI in general) and I'm trying to make a board game with 19x19 squares. They way I'm trying to do this is with 19x19 = 361 QLabel widgets, each with an empty square picture. Then when I want to place a stone at a square, I emit a signal to a slot that changes the PixMap of the QLabel at that square:

    Qt Code:
    1. void MainWidget::placeStone(int y,int x, char colour){
    2.  
    3. if (colour == 'D'){
    4. board[y][x]->setPixmap(QPixmap("black.png"));
    5. } else {
    6. board[y][x]->setPixmap(QPixmap("white.png"));
    7. }
    8. return;
    9. }
    To copy to clipboard, switch view to plain text mode 

    My Class:

    Qt Code:
    1. class MainWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWidget(QWidget *parent = 0);
    6.  
    7. public slots:
    8. void placeStone(int y,int x, char colour);
    9. void moveNotMade(){}
    10. void invalidPort();
    11.  
    12.  
    13.  
    14. private:
    15. QLabel* board [19][19];
    16. };
    To copy to clipboard, switch view to plain text mode 

    The constructor:

    Qt Code:
    1. MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
    2. {
    3. QPushButton *quit = new QPushButton(tr("Quit"));
    4. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    5. myProg* program1 = new myProg();
    6.  
    7. /// PORT INPUT
    8. QLabel *portInputLabel = new QLabel();
    9. portInputLabel->setText("Port (ex: /dev/ttyS0):");
    10. portInputLabel->setAlignment(Qt::AlignRight);
    11. QLineEdit* portInput = new QLineEdit();
    12. connect(portInput,SIGNAL(textChanged(QString)),program1,SLOT(getPort(QString)));
    13.  
    14. /// COLOUR INPUT
    15. QLabel *colourInputLabel = new QLabel();
    16. colourInputLabel->setText("Software AI Colour (dark or light):");
    17. colourInputLabel->setAlignment(Qt::AlignRight);
    18. QLineEdit* colourInput = new QLineEdit();
    19. connect(colourInput,SIGNAL(textChanged(QString)),program1,SLOT(getColour(QString)));
    20.  
    21. QGridLayout *grid = new QGridLayout;
    22. grid->addWidget(portInputLabel,1,1);
    23. grid->addWidget(portInput,1,2);
    24. grid->addWidget(colourInputLabel,2,1);
    25. grid->addWidget(colourInput,2,2);
    26.  
    27. setUpdatesEnabled(true);
    28.  
    29. QPushButton *startButton = new QPushButton(tr("Start"));
    30. connect(startButton, SIGNAL(clicked()),program1,SLOT(startProgram()));
    31.  
    32.  
    33. QGridLayout *grid1 = new QGridLayout;
    34. for (int row = 0; row < 19; ++row) {
    35. for (int column = 0; column < 19; ++column) {
    36. board[row][column] = new QLabel();
    37. board[row][column]->setPixmap(QPixmap("empty.png"));
    38. board[row][column]->adjustSize();
    39. grid1->addWidget(board[row][column], row, column);
    40. }
    41. }
    42. grid1->setHorizontalSpacing(0);
    43. grid1->setVerticalSpacing(0);
    44.  
    45. connect(program1,SIGNAL(placeStone(int,int,char)),this,SLOT(placeStone(int,int,char)),Qt::DirectConnection);
    46. connect(program1,SIGNAL(refreshScreen()),this,SLOT(update()),Qt::DirectConnection);
    47.  
    48. QVBoxLayout *layout = new QVBoxLayout;
    49. layout->addLayout(grid);
    50. layout->addWidget(quit);
    51. layout->addWidget(startButton);
    52. layout->addLayout(grid1);
    53. setLayout(layout);
    54. }
    To copy to clipboard, switch view to plain text mode 

    My Issue is that the placeStone slot only seems to take place AFTER the function that emits the signal returns. I've tried changing the connection type to Qt:irectConnection, but that did not change anything. I've also tried calling the update() function to force it to update the screen, but that didn't help

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit Signal and Slot question

    Can we see the function, that emits the signal? Also update() won't change anything, because
    This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop.
    I guess it is the same with the setPixmap function since it probably also use update().

    Maybe a QGraphicsView with 361 items is better than the overhead of 361 labels which only displays an image.

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Emit Signal and Slot question

    Hi Lykurg, thanks for your response.

    The function that is emitting is just a test function:

    Qt Code:
    1. void myProg::startProgram(){
    2. sleep(2);
    3.  
    4. emit placeStone(1,1,'D');
    5.  
    6. sleep(2);
    7.  
    8. emit placeStone(2,2,'L');
    9.  
    10. sleep(2);
    11.  
    12. emit placeStone(3,3,'D');
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Instead of 1 appearing every 2 seconds, the program waits a total of 6~ seconds then outputs 3 stones at once.

    Anyway, I will look into the QGraphicsView. thanks!
    Last edited by kev.nam; 2nd June 2011 at 16:37.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit Signal and Slot question

    That's because sleep blocks the main gui, thus no update() is processed. To just make sure, that your slot is actually called, add
    Qt Code:
    1. QQCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    after the emits to force the pending updates to be processed.

  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Emit Signal and Slot question

    Never sleep() in the main UI thread. If you need to time operations, use a QTimer or one if it's kin.

    Note that for what you've done so far there's no need to use signal/slot -- you can just call your method. Though you'd probably want to use signals/slots with QTimer.

    You need to understand how virtually all GUI systems work: You set up the details of how you want your interface presented, then "return" to an "event loop" where the actual work takes place. If you never return, the work never takes place. This is why you should never "sleep" -- not only does your work not get done, but any other operations that need to occur are also blocked (possibly including "system" work on behalf of the phone itself, when working on a phone/tablet platform).

Similar Threads

  1. Signal/Slot question
    By sooofunky in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2011, 12:24
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. emit signal from a slot?
    By ask8y@yahoo.com in forum Qt Programming
    Replies: 9
    Last Post: 11th June 2010, 20:18
  4. Signal Slot Question
    By graciano in forum Newbie
    Replies: 8
    Last Post: 19th August 2009, 10:35
  5. Another signal and slot question
    By fnmblot in forum Newbie
    Replies: 5
    Last Post: 4th March 2008, 19:50

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.