c++ - Stretching a QLabel in a QGridLayout -
i manually creating set of qlabels being put qgridlayout, , should distributed evenly. when create test form using qt designer , add series of labels, , put them in qgridlayout, labels occupy full size of cells in grid. when manually in c++, labels don't expand , minimum size text. able these labels expand vertically, not horizontally.
here how i'm creating qgridlayout:
m_layout = new qgridlayout; m_layout->sethorizontalspacing( 1 ); m_layout->setverticalspacing( 1 ); m_layout->setcontentsmargins(0,0,0,0); m_layout->setmargin(0); m_layout->setsizeconstraint( qgridlayout::setdefaultconstraint ); //m_layout->setsizeconstraint( qgridlayout::setmaximumsize ); when change size constraint, not affect size of labels @ all, assumed issue may lie on how i'm creating labels, happens this:
qlabel *label = new qlabel; label->setalignment( qt::aligncenter ); label->setframestyle( qframe::raised ); label->setminimumsize( qsize(0,0) ); label->setmaximumsize( qsize(16777215, 16777215) ); label->setsizepolicy( qsizepolicy::preferred, qsizepolicy::preferred ); right now, qlabels same size, have differing sizes, why i'm using packed-bin algorithm. designed algorithm around classes this:
struct buttonfittingnode { buttonfittingnode( qrect rectangle, qgridlayout *layout ) : used( false ) , node( rectangle ) , down( null ) , right( null ) , m_layout( layout ) { } ~buttonfittingnode() { if ( down ) delete down; if ( right ) delete right; } buttonfittingnode *findnode( qlabel *btn ) { int w = 1; int h = 1; if ( this->used ) { buttonfittingnode *bfn = null; if ( this->right ) bfn = this->right->findnode( btn ); if ( this->down && !bfn ) bfn = this->down->findnode( btn ); return bfn; } else if ( ( w <= node.width() ) && ( h <= node.height() ) ) { qdebug() << "placing @ " << node.x() << node.y(); m_layout->addwidget( btn, node.y(), node.x(), w, h, qt::alignjustify ); this->used = true; this->splitnode( qsize( w, h ) ); return this; } return null; } void splitnode( qsize sz ) { int w = 0, h = 0; // create down node w = this->node.width(); h = this->node.height() - sz.height(); if ( h > 0 ) { qrect n( this->node.x(), this->node.y() + sz.height(), w, h ); this->down = new buttonfittingnode( n, m_layout ); } // create right node w = this->node.size().width() - sz.width(); h = sz.height(); if ( w > 0 ) { qrect n( this->node.x() + sz.width(), this->node.y(), w, h ); this->right = new buttonfittingnode( n, m_layout ); } } bool used; qrect node; buttonfittingnode *down; buttonfittingnode *right; private: qgridlayout *m_layout; }; using 3x2 grid, following output:

sorry, had blur text, since work related project, grey background, white label's background. can see, tall, skinny. want these labels tall , fat. setting of attributes wrong?
this really simple. size constraint of layout has got nothing you're seeing. you're including bunch of useless boilerplate code. rid of explicit setting of minimum , maximum sizes unless have non-default sizes in mind. useless:
label->setminimumsize( qsize(0,0) ); label->setmaximumsize( qsize(16777215, 16777215) ); you need set label's sizepolicy expanding in horizontal direction. that's there's it:
label->setsizepolicy( qsizepolicy::expanding, qsizepolicy::preferred ); i see want bin packing system, won't work as-is, since column/row sizes in grid vary grid gets resized. packer choose column/row spans based on then current row/column sizes. upon resizing, packer's decisions won't adequate anymore.
what want custom layout packing on fly. the flow layout example may close want.
Comments
Post a Comment