简体   繁体   中英

Adjusting QLineEdit's default width

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");
}

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 (eg 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 .

通过使用更广泛的样式表而不是尝试依赖setMinimumSize()函数,我能够使子类化的QLineEdit窗口小部件以25 x 25像素的大小显示,而不QLineEdit窗口小部件限制为固定大小。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM