Jump to content

[Qt]Call for default constructor

- - - - -

  • Please log in to reply
2 replies to this topic

#1
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Hi,

I have problem with Qt Dialog constructor.
While creating a window I want to pass some extra variables but I can't simply add them to constructor...

This is automaticly generated code by qt creator.

c'tor in dialog.h

public:


     explicit Dialog(QWidget *parent=0);

    ~Dialog();


c'tor in dialog.cpp
Dialog::Dialog(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::Dialog)

{

    ui->setupUi(this);

///code

}

 


how window is created in main :
Dialog w;


And this is constructor in QDialog documentation :
QDialog ( QWidget * parent = 0, Qt::WindowFlags f = 0 ) 

My question is, How i can pass extra arguments while creating new object. I tried :
explicit Dialog(QWidget *parent=0,int a, int b);

I also tried to create new ctor by overloading and then call (from initialization list) for this one.

I simply need to pass this to know how many columns and rows user wants to create.

Appreciate any help,
Notes.
Remebre about KISS & DRY

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You could do a couple of things:
1) inherit your form from the QDialog and overload the constructor for extra parameters
2) create an initialize method to do the additional work you need done.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 282 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
You made a wrong in declaration of your 'Dialog' class. The default arguments will be put after the non-default arguments. Below is the correct code.

Class declaration/definition.

class Dialog : public QDialog
{
    Q_OBJECT
public:
      explicit Dialog(int arg1, int arg2, QWidget *parent = 0)
            :QDialog(parent)
      {
      }
};





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users