Setting the default width of QLineEdit

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).

Current way the window appearsDesired way in which the window would appear
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");
}
+4
source share
2 answers

To get a fixed size for yours QLineEdit, you must set sizePolicyto QLineEdit QSizePolicy::Fixed, therefore it QWidget::sizeHint()is the only acceptable alternative, therefore the widget can never grow or contract (for example, the vertical direction of the button).

Take a look at the whole option sizePolicy: http://qt-project.org/doc/qt-5/qsizepolicy.html#Policy-enum .

, ++ GUI Programming with Qt4 .

0

, setMinimumSize(), QLineEdit, 25 25 , .

this->setStyleSheet(QString("border: 1px solid gray; width: 25px; height:25px;"));
0

All Articles