0
votes

I'm very new to Qt and trying to create application, that includes main window, QDockWidget and a button.

Suppose my main window has 1280 x 720 resolution. Then I want to implement QDockWidget that pop up from the left side, width of dockWidth and height of 720 without windowTitleBar. The button has size of (buttonWidth, 720). At first its hidden, and only the button is present, when we click the button dock pops up, button changes position to the right edge of dock. Here is my code:

window::window(unsigned int h, unsigned int v, QWidget *parent) {
    this->setFixedSize(h, v);
    ui.setupUi(this);
    createDockWindow();
}
void window::createDockWindow() {
    dock = new QDockWidget(this);
    dock->setTitleBarWidget(new QMainWindow());
    dock->setGeometry(QRect(this->rect().topLeft(),
                      QSize(dockWidth, this->height())));
    dock->setFloating(true);
    dock->hide();

    path_button = new QPushButton(">", this);
    path_button->setGeometry(QRect(this->rect().topLeft(),
                             QSize(buttonWidth, this->height())));
    connect(path_button, SIGNAL (released()), this, SLOT (showDock()));
}

void rubrick::showDock() {
    if(dock->isHidden()){
        dock->show();
        path_button->setGeometry(QRect(dock->rect().topRight(), 
                                 QSize(buttonWidth, this->height())));
    } else {
        dock->hide();
        path_button->setGeometry(QRect(dock->rect().topLeft(), 
                                 QSize(buttonWidth, this->height())));
    }   
}

So button works perfectly, at first my app looks like that screenshot:

But when the dock shows, it blocks app window title bar, like that: screenshot

I figured, this->rect().topLeft() returns top left of screen, but doesn't take into consideration window Title Bar, I tried to get menuBar height, but it return 30, and I found out that if I move left top by (0, 45) with 0 being width and 45 being height, the dock would be perfectly in place. What am I doing wrong and how to fix that problem?

1

1 Answers

0
votes

The method you're probably looking for is QWidget::frameGeometry, which returns the geometry of the window with the frame included. The rect method returns only the internal area. If you look at QWidget::rect in Qt Assistant, you'll find a link to a "Window Geometry" description that explains all of these interactions reasonably well.