Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Why we write QWidget *parent=0?
Forum Updated to NodeBB v4.3 + New Features

Why we write QWidget *parent=0?

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 5 Posters 3.9k Views 3 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.
  • mit_cruzeM Offline
    mit_cruzeM Offline
    mit_cruze
    wrote on last edited by A Former User
    #1
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0); 
        ~MainWindow();
    
    private slots:
        void on_ok_button_clicked();
        void on_cancel_button_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    

    What is meant by explicit MainWindow(QWidget *parent=0)?
    why we write Qwidget *parent=0?
    Is itokay if we dont write explicit?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!

      • Default arguments
      • explicit specifier

      Cheers!

      1 Reply Last reply
      2
      • J Offline
        J Offline
        Jan-Willem
        wrote on last edited by Jan-Willem
        #3

        When you write:

        explicit MainWindow(QWidget *parent);
        

        you will have to assign a parent, because it expects an argument.
        But when you write:

        explicit MainWindow(QWidget *parent = 0);
        

        you don't have to assign a parent, as it has already a default argument.

        As for the second question, perhaps this gives you some insight?
        https://msdn.microsoft.com/en-us/library/wwywka61.aspx

        1 Reply Last reply
        3
        • thamT Offline
          thamT Offline
          tham
          wrote on last edited by
          #4

          As a side note, nullptr is a better replacement for 0.

          1 Reply Last reply
          1
          • BuckwheatB Offline
            BuckwheatB Offline
            Buckwheat
            wrote on last edited by
            #5

            @tham ... I agree that the use of '0' is not good for pointers as it is ambiguous. But, for < c++11 nullptr is not defined. That being said, Q_NULLPTR could be used and that would be, at least, more readable as to intent.

            Dave Fileccia

            1 Reply Last reply
            1

            • Login

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