PDA

View Full Version : accessing buttons from slot



dlthompson81
15th April 2012, 17:52
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!



#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>

class MyWidget : public QWidget
{
Q_OBJECT

public slots:
void buttonClicked();

public:
MyWidget(QWidget *parent = 0);

};

#endif // MYWIDGET_H





#include "MyWidget.h"

void MyWidget::buttonClicked()
{
//Here is where I need to access the button properties. I never could figure out how to reach the button.


}

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

//This just makes a grid of a few buttons, and plays with some of the options like setting the text etc for testing purposes.

QGridLayout *grid = new QGridLayout;
grid->setSpacing(2);
for (int row = 0; row < 10; ++row) {
for (int column = 0; column < 10; ++column) {
QPushButton *button = new QPushButton;
button->setMinimumSize(5,5);
button->resize(300/20,300/20);
if ( row == 2 ) button->setText("A");
if ( column == 2 ) button->setFlat(true);
button->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding));

connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));

grid->addWidget(button, row, column);
}
}

setLayout(grid);
}





#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>
#include "MyWidget.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
widget.resize(300,300);
return app.exec();
}

wysota
15th April 2012, 18:08
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.

dlthompson81
15th April 2012, 19:47
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.



#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>

class MyWidget : public QWidget
{
Q_OBJECT

//New code declaring an array of pointers to pointers.
QPushButton **md;


public slots:
void buttonClicked();

public:
MyWidget(QWidget *parent = 0);

};

#endif // MYWIDGET_H





#include "MyWidget.h"

void MyWidget::buttonClicked()
{

}

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
//New code that loads an array of pointers into each of the array slots from the variable declared in the h file.
md = new QPushButton*[10];
for( int i = 0; i < 10; i++ ) md[i] = new QPushButton[10];

QGridLayout *grid = new QGridLayout;
grid->setSpacing(2);
for (int row = 0; row < 10; ++row) {
for (int column = 0; column < 10; ++column) {
QPushButton *button = new QPushButton;
button->setMinimumSize(5,5);
button->resize(300/20,300/20);
if ( row == 2 ) button->setText("A");
if ( column == 2 ) button->setFlat(true);
button->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding));

connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));

grid->addWidget(button, row, column);

//Here is my problem. When I try to assign the button into the 2D array, it throws an error.
//The error is: C2679: binary '=' : no operator found which takes a right-hand operand of type 'QPushButton *' (or there is no acceptable conversion)
md[row][column] = button;
}
}

setLayout(grid);
}

wysota
15th April 2012, 20:58
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.

dlthompson81
15th April 2012, 21:04
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.

wysota
15th April 2012, 21:15
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.