Results 1 to 6 of 6

Thread: accessing buttons from slot

  1. #1

    Default accessing buttons from slot

    I made a custom slot for when one of my buttons get clicked on. When this button is clicked, it is to access some other buttons and change some of their properties like setText etc.

    I finally managed to get my button connected with the slot, but I can't figure out how to access the button from the slot. I tried passing the button, passing it by reference, and passing a pointer, but anytime I try to pass something it fails to connect the slot. Here is my current code without passing anything. This is just a test that I've been playing around with.

    I might add that I am a complete newbie at this. I have a programming assignment due Friday that is to make a minesweeper game, so I have a week to learn enough Qt to make it work. I had never even heard of Qt until 2 days ago. What I really need to do is add a unique id to each button so I can access them like that, but I figured I needed to know how to get the slot working first.

    Any help or tips would be much appreciated, Thanks!

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QApplication>
    5. #include <QFont>
    6. #include <QGridLayout>
    7. #include <QPushButton>
    8. #include <QWidget>
    9. #include <QDebug>
    10.  
    11. class MyWidget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public slots:
    16. void buttonClicked();
    17.  
    18. public:
    19. MyWidget(QWidget *parent = 0);
    20.  
    21. };
    22.  
    23. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MyWidget.h"
    2.  
    3. void MyWidget::buttonClicked()
    4. {
    5. //Here is where I need to access the button properties. I never could figure out how to reach the button.
    6.  
    7.  
    8. }
    9.  
    10. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    11. {
    12.  
    13. //This just makes a grid of a few buttons, and plays with some of the options like setting the text etc for testing purposes.
    14.  
    15. QGridLayout *grid = new QGridLayout;
    16. grid->setSpacing(2);
    17. for (int row = 0; row < 10; ++row) {
    18. for (int column = 0; column < 10; ++column) {
    19. QPushButton *button = new QPushButton;
    20. button->setMinimumSize(5,5);
    21. button->resize(300/20,300/20);
    22. if ( row == 2 ) button->setText("A");
    23. if ( column == 2 ) button->setFlat(true);
    24. button->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
    25. QSizePolicy::Expanding));
    26.  
    27. connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    28.  
    29. grid->addWidget(button, row, column);
    30. }
    31. }
    32.  
    33. setLayout(grid);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2. #include <QFont>
    3. #include <QGridLayout>
    4. #include <QPushButton>
    5. #include <QWidget>
    6. #include <QDebug>
    7. #include "MyWidget.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. MyWidget widget;
    13. widget.show();
    14. widget.resize(300,300);
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: accessing buttons from slot

    Store the pointers to all the buttons in an array as a class member of your MyWidget class. And you'll probably want to use QSignalMapper too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: accessing buttons from slot

    Ok, so I tried adding a 2d array to keep track of them, but I seem to be running into a problem assigning the buttons in. I tested it with a 1d array, and it worked fine, so I'm not sure where I'm going wrong. I haven't had much experience with 2d array pointers, so it's possible that it's my c++ code. Anyway, here's my new code. Thanks for looking.

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QApplication>
    5. #include <QFont>
    6. #include <QGridLayout>
    7. #include <QPushButton>
    8. #include <QWidget>
    9. #include <QDebug>
    10.  
    11. class MyWidget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. //New code declaring an array of pointers to pointers.
    16.  
    17.  
    18. public slots:
    19. void buttonClicked();
    20.  
    21. public:
    22. MyWidget(QWidget *parent = 0);
    23.  
    24. };
    25.  
    26. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MyWidget.h"
    2.  
    3. void MyWidget::buttonClicked()
    4. {
    5.  
    6. }
    7.  
    8. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    9. {
    10. //New code that loads an array of pointers into each of the array slots from the variable declared in the h file.
    11. md = new QPushButton*[10];
    12. for( int i = 0; i < 10; i++ ) md[i] = new QPushButton[10];
    13.  
    14. QGridLayout *grid = new QGridLayout;
    15. grid->setSpacing(2);
    16. for (int row = 0; row < 10; ++row) {
    17. for (int column = 0; column < 10; ++column) {
    18. QPushButton *button = new QPushButton;
    19. button->setMinimumSize(5,5);
    20. button->resize(300/20,300/20);
    21. if ( row == 2 ) button->setText("A");
    22. if ( column == 2 ) button->setFlat(true);
    23. button->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
    24. QSizePolicy::Expanding));
    25.  
    26. connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    27.  
    28. grid->addWidget(button, row, column);
    29.  
    30. //Here is my problem. When I try to assign the button into the 2D array, it throws an error.
    31. //The error is: C2679: binary '=' : no operator found which takes a right-hand operand of type 'QPushButton *' (or there is no acceptable conversion)
    32. md[row][column] = button;
    33. }
    34. }
    35.  
    36. setLayout(grid);
    37. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: accessing buttons from slot

    There is no such thing as a 2D array in C/C++. Anyway, what do you need a 2D array for? Your problem is strictly related to C++ (you have types mismatch) and since that's some kind of homework, I can only give you general hints instead of complete solutions. The compiler message says all that needs to be said.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: accessing buttons from slot

    I understand that it's a types mismatch. I can't figure out why though. The array is declared as a QPushButton type. I'm trying to assign a pointer that is pointing to a button which is also a QPushButton type. Maybe I am missing something obvious. I will keep playing with it.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: accessing buttons from slot

    Quote Originally Posted by dlthompson81 View Post
    The array is declared as a QPushButton type.
    Not really. You have a pointer to pointers to QPushButton instances which is equivalent to an array of QPushButton pointers. And since you are using a double dereference operator, you are effectively trying to assign a QPushButton pointer to an instance of QPushButton. That's one of the reasons you shouldn't try to emulate a 2D array with an array of arrays.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how can i write common slot for buttons
    By athulms in forum Newbie
    Replies: 2
    Last Post: 29th September 2011, 22:18
  2. Replies: 2
    Last Post: 26th August 2011, 09:51
  3. Replies: 5
    Last Post: 14th September 2010, 22:13
  4. Replies: 2
    Last Post: 12th June 2010, 03:21
  5. Accessing QAction from within Slot
    By stefanadelbert in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 03:06

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.