i'm trying hand @ writing sudoku solver, trying achieve input of sudoku grid of 9 9 qlineedit
fields.
the grid constructed using grid of 9 qframes
each hold grid of 9 subclassed qlineedit
widgets.
the problem facing cannot find way change default size of qlineedit
widgets 25px 25px without constraining them scaling setting fixed size. have tried resize()
function , subclassing qlineedit
class in order reimplement sizehint()
, can't seem find way adjust initial width of these widgets.
anyone can me out?
below 2 pictures: first of window appears , second 1 how want appear (= same window, after having resized width minimum).
here 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 1px outer border qframe *fullframe = new qframe(this); fullframe->setlinewidth(1); fullframe->setlayout(fullgrid); setcentralwidget(fullframe); setwindowtitle("sudoku"); }
to fixed size of qlineedit
must set sizepolicy
qlineedit
qsizepolicy::fixed
, the qwidget::sizehint()
acceptable alternative, widget can never grow or shrink (e.g. vertical direction of push button).
take @ variant of sizepolicy
: http://qt-project.org/doc/qt-5/qsizepolicy.html#policy-enum.
also, take @ useful , well-wrote book jasmin blanchette , mark summerfield c++ gui programming qt4 , section layout management.
Comments
Post a Comment