Results 1 to 7 of 7

Thread: [SOLVED] Promoted widget and constructor parameters

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default [SOLVED] Promoted widget and constructor parameters

    Hi all,

    I have this scenario:

    I created a custom Database class, then I create an instance of it in main.cpp and i pass it across my app as a const pointer:

    Qt Code:
    1. Database *const m_db = new Database();
    2.  
    3. m_db->createTables();
    4. m_db->exampleData();
    5.  
    6. MyApp(m_db);
    To copy to clipboard, switch view to plain text mode 
    therefore my MyApp constructor is like this:

    Qt Code:
    1. MyApp(Database *const m_db=0, QWidget *parent = 0);
    To copy to clipboard, switch view to plain text mode 

    Now I need to pass the Database pointer to a promoted widget. Is it possible?
    When I try to do that I get an error as in designer Database class is not known, but as my pointer is const I can't call a
    member method to set it, but rather I have to use a member initializer list, something like:

    Qt Code:
    1. class Database;
    2. class Widget : public QWidget
    3. {
    4. public:
    5. Widget(Database *const db, QWidget *parent = 0) : QWidget(parent), m_db(db) {}
    6.  
    7. private:
    8. Database *const m_db;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Is there any other option except to lose principle of least privilege or avoid using promoted widgets?
    I tried googling, but I didn't find anything.

    Thanks all
    Last edited by AlbertoN; 24th October 2013 at 12:36.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Promoted widget and constructor parameters

    Add a setter to your Widget class which takes Database pointer.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Promoted widget and constructor parameters

    I can't use it.

    A const pointer is like a constant and it can be setted into a class only in constructor's member initializer list:

    Qt Code:
    1. MyClass::MyClass(const int foo)
    2. : mfoo(foo) //<--member initializer list
    3. {}
    To copy to clipboard, switch view to plain text mode 

    so my options as far as I know are:

    1. lose principle of least privilege (this means does not use "const")
    2. do not use promotion


    Is there any other way?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Promoted widget and constructor parameters

    Sorry, misread.

    Yep, in that case it's easier to remove const.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Promoted widget and constructor parameters

    Ok thanks.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: [SOLVED] Promoted widget and constructor parameters

    Consider using QSharedPointer if you are sharing a resource between many objects.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Promoted widget and constructor parameters

    Alternative option is to add an empty widget to your UI in designer and add your widget as it child in code, thus being able to pass parameters to its constructor.

    Cheers,
    _

Similar Threads

  1. Replies: 6
    Last Post: 3rd April 2011, 11:15
  2. Why dont see a Custom Widget Promoted ?
    By tonnot in forum Newbie
    Replies: 1
    Last Post: 27th October 2010, 13:24
  3. Replies: 1
    Last Post: 22nd April 2010, 14:34
  4. Replies: 2
    Last Post: 22nd April 2010, 11:44
  5. default parameters in constructor class
    By mickey in forum General Programming
    Replies: 4
    Last Post: 23rd February 2008, 18:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.