I'm trying my hand at writing a sudoku solver, what I am currently trying to achieve is the input of a sudoku into a grid of 9 by 9 QLineEdit
fields.
The grid is constructed by using a grid of 9 QFrames
which each hold a grid of 9 subclassed QLineEdit
widgets.
The problem I am facing is that I cannot find a way to change the default size of the QLineEdit
widgets to 25px by 25px without constraining them from scaling by setting a fixed size. I have tried the resize()
function and subclassing the QLineEdit
class in order to reimplement sizeHint()
, but I can't seem to find a way to adjust the initial width of these widgets.
Anyone who can help me out?
Below are 2 pictures: the first of the window as it currently appears and the second one as how I would want it to appear (= the same window, but after having resized the width to its minimum).
Here is my code:
sudokufield.h
#ifndef SUDOKUFIELD_H
#define SUDOKUFIELD_H
#include <QLineEdit>
class SudokuField : public QLineEdit
{
Q_OBJECT
public:
explicit SudokuField(QWidget *parent = 0);
QSize sizeHint();
};
#endif // SUDOKUFIELD_H
sudokufield.cpp
#include <QtGui>
#include "sudokufield.h"
SudokuField::SudokuField(QWidget *parent) :
QLineEdit(parent)
{
setMinimumSize(25, 25);
setFrame(false);
setStyleSheet(QString("border: 1px solid gray"));
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setValidator(new QIntValidator(1,9,this));
//resize(25,25);
}
QSize SudokuField::sizeHint(){
return QSize(minimumWidth(), minimumHeight());
}
mainwindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "sudokufield.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QGridLayout *fullGrid = new QGridLayout;
fullGrid->setSpacing(0);
//construct 9 big boxes
for(int row(0); row < 3; row++)
for(int column(0); column < 3; column++) {
QFrame *boxFrame = new QFrame(this);
boxFrame->setFrameStyle(QFrame::Box);
boxFrame->setLineWidth(1);
QGridLayout *boxGrid = new QGridLayout;
boxGrid->setMargin(0);
boxGrid->setSpacing(0);
//put 9 subclassed QLineEdit widgets in each box
for(int boxRow(0); boxRow < 3; boxRow++)
for(int boxColumn(0); boxColumn < 3; boxColumn++){
SudokuField *field = new SudokuField(this);
boxGrid->addWidget(field, boxRow, boxColumn);
}
boxFrame->setLayout(boxGrid);
fullGrid->addWidget(boxFrame, row, column);
}
//add another 1px outer border
QFrame *fullFrame = new QFrame(this);
fullFrame->setLineWidth(1);
fullFrame->setLayout(fullGrid);
setCentralWidget(fullFrame);
setWindowTitle("Sudoku");
}
QWidget::setFixedSize(...)
to do what you want. To avoid Mainwindow resizing problem, you should add some spacer on each sides (QSpacerItem). – Curtcurtail