Resizing dynamically created QPushbuttons within a QTableWidget
-
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:
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())); } } }
Thanks in advance!
-
Hi,
Since it's a grid of buttons, why not make use of QGridLayout ?
-
@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/
-
@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. -
@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?_=1696555429922I 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.