Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correct use of setWindowTitle for QWidget object
Forum Updated to NodeBB v4.3 + New Features

Correct use of setWindowTitle for QWidget object

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 732 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    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 .

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    JKSHJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      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 .

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @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");

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Swati777999S 2 Replies Last reply
      3
      • JKSHJ JKSH

        @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");

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #3

        @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!

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        1 Reply Last reply
        0
        • JKSHJ JKSH

          @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");

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #4

          @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?

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          JKSHJ 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @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?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #5

            @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

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved