Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Window Titlebar Height Example



  • After searching for a way to determine the height of window title bars, I finally came up with a way to obtain this value. The example is really clunky, and surely there is a better way to get the height. If you know a better way, please post it!

    mainwindow.h :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    protected:
        void paintEvent(QPaintEvent*);
    
    private:
        QMainWindow *titlebarWindow;
        int  titleBarHeight;
        bool doInit = true;
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp :

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.cpp :

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        qDebug()<<"MainWindow";
    
        titlebarWindow = new QMainWindow(this);
        titlebarWindow->setGeometry(100,100,50,50);
        titlebarWindow->setVisible(true);
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::paintEvent(QPaintEvent*){
    
        if (doInit){
            QPoint myPos = titlebarWindow->pos();
            int yy = titlebarWindow->geometry().y();
            titleBarHeight = yy - myPos.y();
            qDebug()<<"     titleBarHeight "<<titleBarHeight;
            delete titlebarWindow;
            doInit = false;
        }//if
    }
    

  • Lifetime Qt Champion

    Hi
    You can do
    qDebug() << "Height:" << QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight);



  • Lifetime Qt Champion,
    Great! Thanks much!


Log in to reply