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. Resizing dynamically created QPushbuttons within a QTableWidget
QtWS25 Last Chance

Resizing dynamically created QPushbuttons within a QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 380 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.
  • T Offline
    T Offline
    talonxv
    wrote on last edited by talonxv
    #1

    I'm working on a project, creating SOSGameBoard as a QTableWidget and dynamically creating a variable 3x3-10x10 of QPushbuttons. I was wondering if it was possible for the pushbuttons to be resized based on the amount, so that all buttons are visible in the window or if I need to manually change them based on the size. UI picture and code below:

    8b785ecd-6e5f-463f-a898-0492890585fd-image.png

    void SOSGame::createGameBoard(int boardSize) {
        ui->SOSGameBoard->clearContents();
        ui->SOSGameBoard->setFrameStyle((QFrame::NoFrame));
        ui->SOSGameBoard->setRowCount(boardSize);
        ui->SOSGameBoard->setColumnCount(boardSize);
    
        for (int i = 0; i < boardSize; i++){
            for (int j= 0; j < boardSize; j++){
                ui->SOSGameBoard->setItem(i,j, new QTableWidgetItem());
                QPushButton* button = new QPushButton();
                button->setText("");
                ui->SOSGameBoard->setCellWidget(i,j,(QWidget*)button);
                button->sizeIncrement();
                connect(button, SIGNAL(clicked()), this, SLOT(gameBoardButtonClick()));
            }
        }
    }
    

    187f1d6d-ba48-4406-801b-903b5ed163c6-image.png

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since it's a grid of buttons, why not make use of QGridLayout ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Since it's a grid of buttons, why not make use of QGridLayout ?

        T Offline
        T Offline
        talonxv
        wrote on last edited by talonxv
        #3

        @SGaist
        Hi, thanks for the reply. I actually originally implemented it as a QGridLayout but was struggling with creation. I originally had it tied to a vector of QPushButtons, which was likely part of the problem when someone else suggested the QTable Widget. It was been great for what I need, outside of the aesthetics. I think that I need to set each QPushButton's horizontal and vertical size policies to preferred, but not sure if I will be able to implement that within these for loops.

        Edit: Sorry I misread your comment. The QTableWidget is actually in a QGridLayout, however it is not functioning the way my typical QGridLayouts have worked in the past. The QPushButtons are still staying in the top left of the layout/

        jsulmJ 1 Reply Last reply
        0
        • T talonxv

          @SGaist
          Hi, thanks for the reply. I actually originally implemented it as a QGridLayout but was struggling with creation. I originally had it tied to a vector of QPushButtons, which was likely part of the problem when someone else suggested the QTable Widget. It was been great for what I need, outside of the aesthetics. I think that I need to set each QPushButton's horizontal and vertical size policies to preferred, but not sure if I will be able to implement that within these for loops.

          Edit: Sorry I misread your comment. The QTableWidget is actually in a QGridLayout, however it is not functioning the way my typical QGridLayouts have worked in the past. The QPushButtons are still staying in the top left of the layout/

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @talonxv said in Resizing dynamically created QPushbuttons within a QTableWidget:

          The QTableWidget is actually in a QGridLayout

          I don't think this is what @SGaist suggested. He suggested to put buttons in a grid layout without any QTableWidget.
          If you tried it before and it did not work then show the code. Whether you put pointers to buttons in a vector or not doesn't matter.

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

          T 1 Reply Last reply
          0
          • jsulmJ jsulm

            @talonxv said in Resizing dynamically created QPushbuttons within a QTableWidget:

            The QTableWidget is actually in a QGridLayout

            I don't think this is what @SGaist suggested. He suggested to put buttons in a grid layout without any QTableWidget.
            If you tried it before and it did not work then show the code. Whether you put pointers to buttons in a vector or not doesn't matter.

            T Offline
            T Offline
            talonxv
            wrote on last edited by
            #5

            @jsulm
            I see. Thank you.
            https://forum.qt.io/topic/150252/dynamically-creating-a-vector-of-push-buttons-to-a-grid-layout-in-qt-c?_=1696555429922

            I wasn't entirely sure how to track the player movements and scoring (similar to tic tac toe) using a QGridLayout. The code in the link was something I found online, while trying to figure out how to implement it.

            1 Reply Last reply
            0

            • Login

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