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

Correct use of setWindowTitle for QWidget object



  • Hi All,

    I am struggling to correctly use the predefined functions and properties in different classes of Qt.

    I want to set title for my QWidget object but I am unable to do so with setWindowTitle().
    I am sharing my code below, please let me know why the function setWindowTitle does not work in this case.

    mainwindow.h

     #ifndef MAINWINDOW_H
     #define MAINWINDOW_H
     #include <QMainWindow>
     #include <QPushButton>
     #include <QVBoxLayout>
     #include <QHBoxLayout>
     #include<QString>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void setWindowTitle(const QString &title);
     private:
    Ui::MainWindow *ui;
     };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

     #include "mainwindow.h"
     #include "ui_mainwindow.h"
    
     MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
     {
    ui->setupUi(this);
    QWidget *window = new QWidget; // widget object
    window->windowTitle();
       //Creating button objects
       QPushButton *button1 = new QPushButton("One");
       QPushButton *button2 = new QPushButton("Two");
       QPushButton *button3 = new QPushButton("Three");
       QPushButton *button4 = new QPushButton("Four");
       QPushButton *button5 = new QPushButton("Five");
    
       //Creating vertical layout for buttons
      // QVBoxLayout *layout = new QVBoxLayout(window);
    
       //creating horizontal layout for buttons
       QHBoxLayout *layout = new QHBoxLayout(window);
    
       layout->addWidget(button1);
       layout->addWidget(button2);
       layout->addWidget(button3);
       layout->addWidget(button4);
       layout->addWidget(button5);
    
       window->show();
       }
    
     MainWindow::~MainWindow()
    {
     delete ui;
     }
    
     void MainWindow::setWindowTitle(const QString &title)
     {
     QString win_title="Experimenting with Layouts;Vertical and horizontal";
     }
    

    The title of the widget does not change .


  • Moderators

    @Swati777999 said in Correct use of setWindowTitle for QWidget object:

     void MainWindow::setWindowTitle(const QString &title)
     {
     QString win_title="Experimenting with Layouts;Vertical and horizontal";
     }
    

    Call QWidget's implementation of setWindowTitle(). Don't implement your own.

    Remove MainWindow::setWindowTitle() from mainwindow.h and mainwindow.cpp. Then, call this in your constructor: this->setWindowTitle("New title");


  • Moderators

    @Swati777999 said in Correct use of setWindowTitle for QWidget object:

     void MainWindow::setWindowTitle(const QString &title)
     {
     QString win_title="Experimenting with Layouts;Vertical and horizontal";
     }
    

    Call QWidget's implementation of setWindowTitle(). Don't implement your own.

    Remove MainWindow::setWindowTitle() from mainwindow.h and mainwindow.cpp. Then, call this in your constructor: this->setWindowTitle("New title");



  • @JKSH It worked for me. I just added the following in the constructor
    this ->setWindowTitle("Experimenting with the layouts");

    and removed the definition of setWindowTitle.

    I can use window->setWindowTitle(" " ) for setting the title of the widget window.

    Just bear with me as I am an absolute beginner in Qt.

    Thanks for your help!



  • @JKSH

    So to summarise the learning point here; To use the object of MainWindow class in the constructor of MainWindow in mainWindow.cpp file, use ‘this’ as the object of the MainWindow instead of ‘parent’
    To use the member function;
    this -> member_function()

    Just want to be confirmed at this point, members of any class can be accessed by dot operator, right?


  • Moderators

    @Swati777999 said in Correct use of setWindowTitle for QWidget object:

    So to summarise the learning point here; To use the object of MainWindow class in the constructor of MainWindow in mainWindow.cpp file, use ‘this’ as the object of the MainWindow instead of ‘parent’
    To use the member function;
    this -> member_function()

    Yes.

    Note also that this is implied. The 2 calls to member_function() below do exactly the same thing:

    void MyClass::func() {
        this->member_function(); // 1st call
        member_function(); // 2nd call
    }
    

    Just want to be confirmed at this point, members of any class can be accessed by dot operator, right?

    Yes, unless you have a pointer to the object. For pointers, you use -> not .

    Note: this is a pointer


Log in to reply