March 31, 2012

notgary notgary
Lab Rat
37 posts

Adding a QSizeGrip to the corner of a QLabel

 

Hi there,

I’m attempting to produce a widget that consists of a text display that can be resized by the user grabbing the lower right corner. So far I’ve been able to generate this:

Resizeable label

I’ve applied a read background to the layout to make it more obvious what’s going on. I’ve used the following code to generate this:

  1.   m_sizeGrip = new QSizeGrip( this );
  2.   m_layout = new QHBoxLayout( this );
  3.   m_label = new QLabel( this );
  4.  
  5.   m_layout->setContentsMargins( QMargins() );
  6.   m_layout->setSpacing( 0 );
  7.   m_layout->addWidget( m_label );
  8.   m_layout->addWidget( m_sizeGrip, 0, Qt::AlignBottom | Qt::AlignRight );
  9.  
  10.   setWindowFlags( Qt::SubWindow );

Basically, it’s a horizontal layout with the label and grip added to it, which is then installed on a QWidget. My problem is that I’d like the grip to be on the lower right corner of the label, rather than the parent widget. I’d also like to make it invisible while keeping it enabled.

Or perhaps I’m going about this the wrong way. My ultimate goal is to have a textual display widget that can be resized by the user either horizontally or vertically, but doesn’t have a visible grip that would obscure the text. Am I already on the right track with the code above, or is there a better way to achieve this?

1 reply

April 2, 2012

gmaro gmaro
Lab Rat
50 posts

Hi!

If you like easy and lazy solutions:
put a QLablel inside the QDialog (instead of your QWidget) and enable the SizeGrip (QDialog has built-in sizeGripEnabled property).

I think that’s the easiest way.

Hope that helps,
Marek

 
  ‹‹ Change style      Qt Creator double spin box possible broken[inprecise values] ››

You must log in to post a reply. Not a member yet? Register here!