Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting QLineEdit's default width

Tags:

c++

qt

qlineedit

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

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");
}
like image 610
mr_kazz Avatar asked Nov 11 '22 02:11

mr_kazz


1 Answers

By using a more extensive stylesheet instead of trying to rely on the setMinimumSize() function I was able to get the subclassed QLineEdit widgets to appear at a size of 25 by 25 pixels without limiting the widget to a fixed size.

this->setStyleSheet(QString("border: 1px solid gray; width: 25px; height:25px;"));
like image 95
mr_kazz Avatar answered Nov 15 '22 13:11

mr_kazz