[Untitled] QT custom close, zoom in, zoom out button title bar

Developed a Fourier cycle analysis software by myself, which can be used for cycle analysis and trend analysis of stocks, funds, futures, foreign exchange and other data;

1. Cause of the problem

Because it is necessary to change the color skin of the title bar of the Qt program, add header instructions, and authorization instructions, but the color of the title bar that comes with QT seems to be unable to be changed, and it is even more difficult to add new button text, so I customize a program. title. As shown below.

 2. Implementation steps

2.1 Customize the title bar interface

2.2 Hide the title bar that comes with the system

//Set to borderless, custom close, zoom in, zoom out button
this->setWindowFlags(Qt::FramelessWindowHint);

2.3 Realize close, maximize, minimize, button response

// top button response
    connect(ui.pBClose, &QPushButton::clicked, [=]() {close(); });
    connect(ui.pBMaxWindow, &QPushButton::clicked, [=]()
    {    if (this->isMaximized())
    {
        this->showNormal();
    }
    else
    {
        this->showMaximized();
    }
    });
    connect(ui.pBMinWindow, &QPushButton::clicked, [=]() {this->showMinimized(); });

2.4 Realize holding down the title bar and dragging the window to move

The title bar of the system comes with the effect of holding down the left mouse button to drag the window, and this function is also discounted here

When the mouse is pressed, it is judged whether the mouse is on the title bar. If yes, the left mouse button is set to true. The mouse movement function internally judges whether the mouse is pressed, calculates the real-time position of the mouse, and the initial position For comparison, calculate the relative displacement of the mouse movement, then add the relative displacement to the coordinates of the main window, and then move the main window to update the starting position. In the mouse release function, just set the left button down boolean value to false;

void FuliyePeriod::mousePressEvent(QMouseEvent * event)
{
    //Only the left mouse button can move and change the size
    if (event->button() == Qt::LeftButton)
    {
        if (ui.widgetToolBar->underMouse())
        {
            m_leftMousePressed = true;
            //When the left mouse button is pressed, the coordinates of the window on the screen
            m_StartPoint = event->globalPos();
        }
    }
}
void FuliyePeriod::mouseMoveEvent(QMouseEvent *event)
{
    //Move the window
    if (m_leftMousePressed)
    {
        QPoint curPoint = event->globalPos(); //The position when pressing and moving
        QPoint movePoint = curPoint - m_StartPoint;
        // normal window
        QPoint mainWinPos = this->pos();
        this->move(mainWinPos.x() + movePoint.x(), mainWinPos.y() + movePoint.y());
        m_StartPoint = curPoint;
 
    }
}
void FuliyePeriod::mouseReleaseEvent(QMouseEvent * event)
{
    m_leftMousePressed = false;//Release the mouse
}

Finally, the function of dragging the main window by pressing and holding the title bar is realized; there is another problem that the system border can be dragged to adjust the size, we will see how to realize it in the next section;

Guess you like

Origin blog.csdn.net/baochunlei1/article/details/124533263