PDA

View Full Version : Resize a QDockWidget ? (Width)



Selven
28th April 2012, 18:03
Hi,
I have a problem with my QDockWidget, I want it to take all available width space. But it only takes the minimum space...

This is the source code that I do :


//testWidget.h
class testWidget : public QWidget
{
Q_OBJECT

public:
explicit testWidget(QWidget *parent = 0);
QLabel test;
QPushButton testButton;

};


//testWidget.cpp
testWidget::testWidget(QWidget *parent) :
QWidget(parent)
{
setStyleSheet("background-color: yellow");

test.setText("test label");
test.setParent(this);
test.setVisible(true);

testButton.setText("TestButton");
testButton.setParent(this);
testButton.move(0, 20);
testButton.setVisible(true);
}


//mainWindow.h
class mainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit mainWindow(QWidget *parent = 0);
testWidget *testW;
};


//mainWindow.cpp
mainWindow::mainWindow(QWidget *parent)
: QMainWindow(parent)
{
setFixedSize(1000, 900);

QLabel *test = new QLabel;
test->setFixedSize(500,900);
test->setStyleSheet("background-color: black;");
setCentralWidget(test);

testW = new testWidget;

QDockWidget *dock = new QDockWidget("DOCKTEST", this);
dock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
dock->setWidget(testW);
addDockWidget(Qt::RightDockWidgetArea, dock);
}

The result is:
7650

And I want that :
7651

I don't know how to do that, and if I add 2 widgets in the QDockWidget, how can I only define the height value ? (maybe 2/3 for the first one, and 1/3 for the second one)

So, if someone can help me, it will be very great !

Thanks in advance for your help,

sedi
28th April 2012, 21:10
Have you tried this?

setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Ma ximum);
setMinimumSize(/*whatever*/);
setMaximumSize(/*whatever*/);
//or:
// setMinimumHeight(/*whatever*/);
// setMinimumWidth(/*whatever*/);
// same for Maximum

Selven
28th April 2012, 22:48
Hi,

If I put this, it works :

//testWidget.cpp
setMinimumWidth(500);

But if i add 2 widgets, for example, the first one (height : 700), and the second one (height : 200).

How can I set this value ? If I use setMinimumHeight, widgets has the good value but can't be resizeable less...
So I want to define the height and the widget can eventually be resized. It's just the default value, so, this can be change !

Thanks for your help,

ChrisW67
28th April 2012, 23:29
You have not asked Qt to do any sort of Layout Management so consequently it does not do any layout management. Widgets stay at the size and location you give them and nothing adjusts automatically. Here is a working example:


#include <QtGui>

class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *p = 0): QWidget(p) {
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("A label", this));
layout->addWidget(new QPushButton("A button", this));
layout->addStretch();
setLayout(layout);
// setMinimumWidth(200); // experiment here if you want to constrain the size
}
};

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this);
central->setStyleSheet("* { background-color: blue; }");
setCentralWidget(central);

QDockWidget *dw = new QDockWidget("DockWidget", this);
dw->setWidget(new Widget(dw));
addDockWidget(Qt::RightDockWidgetArea, dw);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.resize(640, 480);
m.show();
return app.exec();
}
#include "main.moc"

Selven
29th April 2012, 09:31
It's true, i don't define a layout for the widget... It's done now.

I have another problem, the first window has autofilbackground value to true, and a black background. So, the context menu of the QDockWidget is black, and I can't see the text... When I right click on the title bar of the QDockWidget, i have a context menu for set visible or not, the QDockWidget, (in clicking checkbox). This context menu have a background color black and a text color black too. So I can see it.
How can i modify this parameter ?

Thanks in advance for your help

sedi
29th April 2012, 13:54
I'm not sure if I understand you correctly. But it seems to me that you could use the Style Selectors to change the appearance of only those widgets you want to change - and not their children:
http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html