Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QObject Problem with using sender() to array of buttons
Forum Updated to NodeBB v4.3 + New Features

QObject Problem with using sender() to array of buttons

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 724 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MTWK
    wrote on 23 Apr 2018, 18:18 last edited by
    #1

    Hello everyone,

    I have some problems with programming a qt application. It's a type of game called "Tic Tac Toe".
    The main problem is that after clicking any of button the program is not responding. I guess it's about wrong using sender() function.
    Here is my code from mainwindow.cpp:

    #include <QtWidgets>
    #include <QtGui>
    #include <QGridLayout>
    #include <QtCore>
    #include <QPushButton>
    #include <QObject>
    #include <QString>
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #define A 3
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
       // ui->setupUi(this);
        Draw(A);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    
    }
    
    MainWindow::Draw(int a)
    {
            int count=1;
            int i, j;
            QGridLayout *layout = new QGridLayout;
            QWidget *window = new QWidget;
            QPushButton *Button[a][a];
    
            for(i=0;i<a;i++)
            {
                for(j=0;j<a;j++)
                {
                    if(count<=(a*a))
                    {
                        Button[i][j] = new QPushButton(QString::number(count));
                        Button[i][j]->setMinimumWidth(100);
                        Button[i][j]->setMinimumHeight(100);
                        Button[i][j]->setText(QString::number(count));
                        Button[i][j]->setProperty("s", i);
                        Button[i][j]->setProperty("d", j);
                        layout->addWidget(Button[i][j],i,j);
                        connect(Button[i][j],SIGNAL(clicked(bool)),this,SLOT(test()));
                        window->setLayout(layout);
                        count++;
                    }
                }
            }
    
            window->show();
            return 0;
    
    }
    
    void MainWindow::test()
    {
        QObject * pSender = sender();
        int s = pSender->property("s").toInt();
        int d = pSender->property("d").toInt();
        if(turn%2==0)
        {
            Button[s][d]->setStyleSheet("{ border-image: url(:x.png);}");
            Button[s][d]->setEnabled(false);
        }
        else
        {
            Button[s][d]->setStyleSheet("{ border-image: url(:kolko.jpg);}");
            Button[s][d]->setEnabled(false);
        }
       turn++;
    }
    
    

    my code from header mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QApplication>
    #include <QGridLayout>
    #include <QtGui>
    #include <QtCore>
    #include <QtWidgets>
    
    namespace Ui {
    class MainWindow;
    class ButtonWidget;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        int Draw(int b);
        void test();
    
    signals:
       clicked(bool);
    
    private:
        Ui::MainWindow *ui;
        QPushButton *Button[3][3];
        int count=1;
        int i, j;
        int turn=0;
    };
    
    #endif // MAINWINDOW_H
    

    and my main.cpp if u need:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QGridLayout>
    #include <QtGui>
    #include <QtCore>
    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        return a.exec();
    }
    
    

    I would really appreciate if you could help me.

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 23 Apr 2018, 18:23 last edited by
      #2

      @MTWK said in QObject Problem with using sender() to array of buttons:

      QPushButton *Button[a][a];

      You define it twice ...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 23 Apr 2018, 18:33 last edited by
        #3

        Hi
        as mr @Christian-Ehrlicher says
        it seems you have both
        MainWindow::Draw(int a) {
        QPushButton *Button[a][a]; // local to Draw

        and
        private:
        Ui::MainWindow *ui;
        QPushButton *Button[3][3]; // class member and not the one in Draw

        1 Reply Last reply
        2

        1/3

        23 Apr 2018, 18:18

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved