Adjusting QLineEdit's default width
Asked Answered
Q

2

6

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 appears Desired 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");
}
Quintal answered 24/8, 2014 at 13:43 Comment(2)
Hi, if your mainwindow appears like a rectangle, it's probably because you leave a setSize() somewhere or it's the size you defined in the Ui. How you can use QWidget::setFixedSize(...) to do what you want. To avoid Mainwindow resizing problem, you should add some spacer on each sides (QSpacerItem).Curtcurtail
I have not set a size on the window in the ui editor (I have no ui file) nor using the setSize() method. I do not want a fixed size for the widgets, I want the QLineEdits to initialize to 25px x 25px squares and scale upwardsQuintal
Q
2

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;"));
Quintal answered 24/8, 2014 at 19:23 Comment(0)
N
0

To get fixed size of your QLineEdit you must set sizePolicy for your QLineEdit to QSizePolicy::Fixed, so the QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).

Take a look at all variant of sizePolicy: http://qt-project.org/doc/qt-5/qsizepolicy.html#Policy-enum.

Also, take a look at very useful and well-wrote book by Jasmin Blanchette and Mark Summerfield C++ GUI Programming with Qt4 and its section about Layout Management.

Neutron answered 24/8, 2014 at 14:37 Comment(4)
I don't want a fixed size, I want to set an initial size. I have tried subclassing the QLineEdit to get a hold on [sizeHint()], but I must have done something wrong I think. It is also what is at the end of the article you referred to.Quintal
@Quintal but do you want the cells to always remain square?Swafford
@Swafford its not really in my question, but that would be great! :)Quintal
@mr_kazz, Just read all links in answer and you'll be able to do all you want, and avoid inappropriate usage of css styles.Neutron

© 2022 - 2024 — McMap. All rights reserved.