I am trying my best to write sudoku written decisions, what I am currently trying to do is to enter sudoku into a 9 by 9 grid of fields QLineEdit.
The grid is constructed using a grid of 9 QFrames, each of which contains a grid of 9 subclasses QLineEdit.
The problem I am facing is that I cannot find a way to change the default size of the widget QLineEditto 25 pixels by 25 pixels without limiting their scaling by setting a fixed size. I tried the function resize()and subclassed the class QLineEditto override sizeHint(), but I can not find a way to adjust the initial width of these widgets.
Who can help me?
Below are 2 images: the first window that is currently being displayed, and the second, as I want it to appear (= the same window, but after changing the width to a 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.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));
}
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);
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);
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);
}
QFrame *fullFrame = new QFrame(this);
fullFrame->setLineWidth(1);
fullFrame->setLayout(fullGrid);
setCentralWidget(fullFrame);
setWindowTitle("Sudoku");
}
source
share