PDA

View Full Version : Overriding QPlainTextEdit's sizeHint with a smaller minimumSize



Phlucious
5th January 2011, 01:54
Greetings! Longtime reader, first time poster (etc). I'm a civil engineer who's been learning to program in Qt (v4.7.0, in Creator 2.0.1) over the past year or so, so I'm probably just missing something obvious.

I have a simple Qt core GUI MainWindow class that I've put together for my lab as follows:

#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QFile>
#include <QString>
#include <QCoreApplication>
#include <QPlainTextEdit>


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//disable the Windows maximize button
setWindowFlags( windowFlags() & ~Qt::WindowMaximizeButtonHint );

// create a minimalistic layout with just a text window and button
text_ViewLog = new QPlainTextEdit();
text_ViewLog->setMinimumSize(100,100); //sets the minimimum (and default) widget size (optional)
text_ViewLog->setReadOnly(true); //prevents editing by the user (required when being used as a display widget)
text_ViewLog->setTabChangesFocus(true); //allows user to tab-cycle through the objects (Tab does nothing if false and it's ReadOnly)
text_ViewLog->setLineWrapMode(QPlainTextEdit::NoWrap); //prevents wordwrap so that each line of text is a unique message (optional)
text_ViewLog->setPlainText("Hello"); //sample starter text (optional. note that keeping this blank will clear the display)
text_ViewLog->appendHtml("<b>World</b>"); //sample addition of html-formatted text
// text_ViewLog->setFont(QFont("Courier")); //sample font change
// text_ViewLog->setBackgroundVisible(true);
text_ViewLog->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
pushbutton_Start = new QPushButton(tr("Start"));
pushbutton_Start->setFixedWidth(60); //button heights are automatically fixed. width varies with the layout, by default
layout_Main = new QVBoxLayout; //build the layout. every component widget & layout must be added individually
layout_Main->addWidget(text_ViewLog);
layout_Main->addWidget(pushbutton_Start);
layout_Main->setAlignment(pushbutton_Start, Qt::AlignHCenter); //centers the button horizontally. aligns left by default if the object has a fixed/max width.
centralWidget = new QWidget(this); //the centralWidget MUST be created & assigned for a MainWindow to be displayed
centralWidget->setLayout(layout_Main);
this->setCentralWidget(centralWidget);

// weave signals and slots
connect(pushbutton_Start, SIGNAL(clicked()), this, SLOT(pushbutton_Start_onClicked())); //sample signal-slot assignment. see below to see the slot definition
}

MainWindow::~MainWindow()
{

}

void MainWindow::pushbutton_Start_onClicked()
{
this->userUpdate("This Is A Really Long Line This Is A Really Long Line This Is A Really Long Line This Is A Really Long Line This Is A Really Long Line");
}

void MainWindow::userUpdate(QString t)
{
this->text_ViewLog->appendPlainText(t);
}

My intended behavior is that text_ViewLog (a QPlainTextEdit) would default to its minimumSize (100x100) when the window is first displayed. However, what's happening is that text_ViewLog initially displays with dimensions equal to either the minimum, or the sizeHint() (256x192), whichever is higher. In this case, since 100x100 is lower than the sizeHint on both counts, it defaults to 256x192 when the window is first displayed. The user can still manually resize the window to 100x100 after it launches, but I want it to default to the minimum.

As I understand it, it's behaving as if the SizePolicy is (Preferred,Preferred), but I changed it to (MinimumExpanding,MinimumExpanding)... I thought. Am I missing something? Calling text_ViewLog.updateGeometry() in the constructor has no effect. I didn't see any sort of text_ViewLog.setSizeHint()-type method.

Thanks!

-Phlucious

PS—I only posted the CPP file since the header is so basic. If needed, I can post the header, too.