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>
{
Q_OBJECT
public slots:
void buttonClicked();
public:
};
#endif // MYWIDGET_H
#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
To copy to clipboard, switch view to plain text mode
#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.
}
{
//This just makes a grid of a few buttons, and plays with some of the options like setting the text etc for testing purposes.
grid->setSpacing(2);
for (int row = 0; row < 10; ++row) {
for (int column = 0; column < 10; ++column) {
button->setMinimumSize(5,5);
button->resize(300/20,300/20);
if ( row == 2 ) button->setText("A");
if ( column == 2 ) button->setFlat(true);
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
grid->addWidget(button, row, column);
}
}
setLayout(grid);
}
#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);
}
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include <QFont>
#include <QGridLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>
#include "MyWidget.h"
int main(int argc, char *argv[])
{
MyWidget widget;
widget.show();
widget.resize(300,300);
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks