Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach a 'descending panel' to the bottom of the Menubar in QMainWindow

Tags:

c++

qt4

When you save a file on a Mac, a panel kinda descends down from the top bar in a really cool way. I want to create a class that does a similar thing using the Qt framework. There are a number of things that I'm confused about:

  1. When the panel descends, input to the parent window should be blocked. This is easy with QDialog as it has the setModal() method, however - QDialogs, by default pop-out. I'm not sure how to get around this.

  2. In a QMainProject, there is a QMenua new instance of the DescendingPanel class is created. How would you do that, supposing there are other widgets below the menubar. The DescendingPanel should appear above them.

I would really appreciate any help with this.

EDIT

I had an idea that instead of pegging the dialog under the menubar, just make it appear under there and remove the window frame. That way, it would give an illusion that it popped out from under there. Ofcourse, Move events would also have to be handled so that the Dialog is always under the menubar but that's for later. Here's the code I used to get the DescendingDialog to appear under the menubar.

class DescendingDialog : public QWidget
{
    QMainWindow* Window;
    QWidget*     Menu;
    QPoint       GlobalLocationOfMenu;
    int          DialogWidth;
    int          DialogHeight;

    int X()
    {
        int XDistanceOfPanel = GlobalLocationOfMenu.x() + ((Menu->width()/2) - (this->DialogWidth/2));
        //GlobalLocationOfMenu.x() returns 0;
        return XDistanceOfPanel;
    }

    int Y()
    {
        int YDistanceOfPanel = GlobalLocationOfMenu.y()+Menu->height();
        //GlobalLocationOfMenu.y() returns 0;
        return YDistanceOfPanel;
    }

    void SetGeometry()
    {
        this->setGeometry(this->X(),this->Y(),this->DialogWidth,this->DialogHeight);

    }
public:
    DescendingDialog(QMainWindow*   Window,int DialogWidth,int DialogHeight):QWidget(NULL)
    {
       this->Window = Window;
       this->Menu   = this->Window->menuWidget();
       this->DialogWidth = DialogWidth;
       this->DialogHeight = DialogHeight;

       QPoint RelativeLocationOfMenu = this->Menu->pos();
       this->GlobalLocationOfMenu = QWidget::mapToGlobal(RelativeLocationOfMenu);
       this->SetGeometry();

    }
};

It didn't work because the GlobalLocationOfMenu.x() and .y() returned 0 so the dialog doesn't appear where I want it to.

like image 615
W.K.S Avatar asked Dec 18 '11 10:12

W.K.S


People also ask

What is the difference between QMainWindow and QWidget?

A QDialog is based on QWidget , but designed to be shown as a window. It will always appear in a window, and has functions to make it work well with common buttons on dialogs (accept, reject, etc.). QMainWindow is designed around common needs for a main window to have.

How do I add menu bar to QT?

In most main window style applications you would use the menuBar() function provided in QMainWindow, adding QMenus to the menu bar and adding QActions to the pop-up menus. Example (from the Menus example): fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct);

What is central widget in Qt?

Creating Main Window Components. A central widget will typically be a standard Qt widget such as a QTextEdit or a QGraphicsView. Custom widgets can also be used for advanced applications. You set the central widget with setCentralWidget() . Main windows have either a single (SDI) or multiple (MDI) document interface.


1 Answers

You can let a dialog "slide in" by using a function similar to that:

#include <QDialog>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>

void makeAppear(QDialog * dialog, QRect geometryEnd)
{
    static QParallelAnimationGroup *animationGroup = 0;
    if (animationGroup)
    {
        for(int i = 0, ie = animationGroup->animationCount(); i != ie; ++i)
            delete animationGroup->animationAt(i);
        delete animationGroup;
    }

    // Set up start and end geometry for 'dialog'.
    QPoint parentTopLeft = dialog->parentWidget()->geometry().topLeft();
    geometryEnd.translate(dialog->parentWidget()->mapToGlobal(parentTopLeft));
    QRect geometryBegin = geometryEnd;
    geometryBegin.setHeight(0);

    // Set up start and end geometry for the only child widget of 'dialog'.
    QWidget * dialogChildWidget = dynamic_cast< QWidget * >(dialog->children().first());
    if ( !dialogChildWidget )
        return;
    QRect childGeometryEnd = dialogChildWidget->geometry();
    QRect childGeometryBegin = childGeometryEnd;
    childGeometryBegin.translate(0, geometryEnd.height() * (-1));

    // Set up animation for 'dialog'.
    QPropertyAnimation *dialogAnimation = new QPropertyAnimation(dialog, "geometry");
    dialogAnimation->setDuration(400);
    dialogAnimation->setStartValue(geometryBegin);
    dialogAnimation->setEndValue(geometryEnd);

    // Set up animation for the only child widget of 'dialog'.
    QPropertyAnimation *childAnimation = new QPropertyAnimation(dialogChildWidget, "geometry");
    childAnimation->setDuration(400);
    childAnimation->setStartValue(childGeometryBegin);
    childAnimation->setEndValue(childGeometryEnd);

    // Set up (and start) a parallel animation group
    animationGroup = new QParallelAnimationGroup;
    animationGroup->addAnimation(dialogAnimation);
    animationGroup->addAnimation(childAnimation);
    animationGroup->start();

    // Make 'dialog' visible, borderless, modal.
    dialog->setModal(true);
    dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    dialog->show();
}

The dialog argument shall point to a (hidden/not visible) QDialog instance with one single child widget, that contains all other widgets that belong to the dialog.

The geometryEnd argument shall specify position and size of dialog after it has appeared (relative to it's parent widget).

The result looks like this.

like image 60
Baltram Avatar answered Nov 10 '22 00:11

Baltram