Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. PushButtoin in ein 2D Array?

PushButtoin in ein 2D Array?

Scheduled Pinned Locked Moved Unsolved German
2 Posts 2 Posters 287 Views
  • 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
    Martin H
    wrote on last edited by
    #1

    Hallo Zusammen,

    hat jemand eine Idee, wie man ein 2D Array von Pushbuttons
    über einen Connect verbindet, welches auf einen einzige Slot
    zeigt, welcher für alle QPushButton zuständig ist.

    Ich habe eine Reihe von Buttons, welche die gleiche Aufgabe
    erledigen sollen.

    Ich programmiere für ein Schulprojekt TicTacToe.
    Ich habe dynamisch ein Gridlayout erzeugt, welches ein 3x3 Spielfeld aus PushButtons hat.

    Ich möchte die Buttons in ein Array laden, um es leichter auszuwerten welcher Spieler eine Reihe voll hat :)
    Ich habe gehört, dass es anscheinend besser gehen soll die Buttons in ein Array zu laden, wenn man das Spielfeld dynamisch anlegt.

    code_text
    
    TTT_Test::TTT_Test(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::TTT_Test)
    {
        ui->setupUi(this);
    
    
        // Grid layout with 9 buttons
           QGridLayout *gridLayout = new QGridLayout;
           QPushButton *b1 = new QPushButton();
           QPushButton *b2 = new QPushButton();
           QPushButton *b3 = new QPushButton();
           QPushButton *b4 = new QPushButton();
           QPushButton *b5 = new QPushButton();
           QPushButton *b6 = new QPushButton();
           QPushButton *b7 = new QPushButton();
           QPushButton *b8 = new QPushButton();
           QPushButton *b9 = new QPushButton();
    
           // addWidget(*Widget, row, column, rowspan, colspan)
           // 0th row
           gridLayout->addWidget(b1,0,0,1,1);
           gridLayout->addWidget(b2,0,1,1,1);
           gridLayout->addWidget(b3,0,2,1,1);
    
           // 1st row with 2-column span
           gridLayout->addWidget(b4,1,0,1,1);
           gridLayout->addWidget(b5,1,1,1,1);
           gridLayout->addWidget(b6,1,2,1,1);
    
    
    
           // 2rd row with 3-column span
           gridLayout->addWidget(b7,2,0,1,1);
           gridLayout->addWidget(b8,2,1,1,1);
           gridLayout->addWidget(b9,2,2,1,1);
    
           // Create a widget
           QWidget *w = new QWidget();
    
           // Set the grid layout as a main layout
           w->setLayout(gridLayout);
    
           w->setStyleSheet("QPushButton { background-color: white;"
                            "height:135px;"
                            "width:150px;"
                            "border: 1px solid  black;}");
    
           // Display
           w->show();
    
    jsulmJ 1 Reply Last reply
    0
    • M Martin H

      Hallo Zusammen,

      hat jemand eine Idee, wie man ein 2D Array von Pushbuttons
      über einen Connect verbindet, welches auf einen einzige Slot
      zeigt, welcher für alle QPushButton zuständig ist.

      Ich habe eine Reihe von Buttons, welche die gleiche Aufgabe
      erledigen sollen.

      Ich programmiere für ein Schulprojekt TicTacToe.
      Ich habe dynamisch ein Gridlayout erzeugt, welches ein 3x3 Spielfeld aus PushButtons hat.

      Ich möchte die Buttons in ein Array laden, um es leichter auszuwerten welcher Spieler eine Reihe voll hat :)
      Ich habe gehört, dass es anscheinend besser gehen soll die Buttons in ein Array zu laden, wenn man das Spielfeld dynamisch anlegt.

      code_text
      
      TTT_Test::TTT_Test(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::TTT_Test)
      {
          ui->setupUi(this);
      
      
          // Grid layout with 9 buttons
             QGridLayout *gridLayout = new QGridLayout;
             QPushButton *b1 = new QPushButton();
             QPushButton *b2 = new QPushButton();
             QPushButton *b3 = new QPushButton();
             QPushButton *b4 = new QPushButton();
             QPushButton *b5 = new QPushButton();
             QPushButton *b6 = new QPushButton();
             QPushButton *b7 = new QPushButton();
             QPushButton *b8 = new QPushButton();
             QPushButton *b9 = new QPushButton();
      
             // addWidget(*Widget, row, column, rowspan, colspan)
             // 0th row
             gridLayout->addWidget(b1,0,0,1,1);
             gridLayout->addWidget(b2,0,1,1,1);
             gridLayout->addWidget(b3,0,2,1,1);
      
             // 1st row with 2-column span
             gridLayout->addWidget(b4,1,0,1,1);
             gridLayout->addWidget(b5,1,1,1,1);
             gridLayout->addWidget(b6,1,2,1,1);
      
      
      
             // 2rd row with 3-column span
             gridLayout->addWidget(b7,2,0,1,1);
             gridLayout->addWidget(b8,2,1,1,1);
             gridLayout->addWidget(b9,2,2,1,1);
      
             // Create a widget
             QWidget *w = new QWidget();
      
             // Set the grid layout as a main layout
             w->setLayout(gridLayout);
      
             w->setStyleSheet("QPushButton { background-color: white;"
                              "height:135px;"
                              "width:150px;"
                              "border: 1px solid  black;}");
      
             // Display
             w->show();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Martin-H said in PushButtoin in ein 2D Array?:

      Gridlayout erzeugt, welches ein 3x3

      dann solltest du auch in der Lage sein QPushButton* in einen 2D Vector zu packen, oder?
      Wegen Slot: benutze Lambda-Functions, dann kannst du auch x/y für jeden Button mit nehmen.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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