Results 1 to 3 of 3

Thread: A basic C++ question about Actions

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default A basic C++ question about Actions

    I see how works the menu system at tutorial 'recent files'.

    ( This is a piece of code)
    at .h
    Qt Code:
    1. enum { MaxRecentFiles = 5 };
    2. QAction *recentFileActs[MaxRecentFiles];
    To copy to clipboard, switch view to plain text mode 
    at .cpp
    Qt Code:
    1. recentFilesActs[0]= new QAction("a_string", this);
    To copy to clipboard, switch view to plain text mode 

    There is not problem in having recentFileActs with 5 'reserved' QActions
    But, if I didnt want to have 'reserved' space and want to create at .cpp ?
    It is a basic c++ question but I dont know how to do it. It is related with 'How can I create an array of objects that have a constructor parameter'.

    At h.file I want : QAction *recentFileActs[];
    and at .cpp : ???? code for variable and exact value
    I can't write recentFileActs = new QActions(20);


    1.- Have I to create a QAction by pointer and later to pass it to recentFileActs[0] ?
    2.- In that case must I to keep in mind that I have to delete the objects created?

    Thanks
    Last edited by high_flyer; 16th February 2011 at 14:23. Reason: code tags

  2. #2
    Join Date
    Dec 2009
    Posts
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A basic C++ question about Actions

    Use QVector instead of a straight array

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A basic C++ question about Actions

    With the original tutorial and the:
    Qt Code:
    1. QVector<QAction *> recentFileActs; // if you want a particular size set at run time. Look at QVector::resize()
    2. QList<QAction*> recentFileActs; // if you want an open ended list (not likely for an MRU list)
    To copy to clipboard, switch view to plain text mode 
    suggestions you need to make sure the QActions are deleted. Usually this is by giving each QAction a QObject parent when you construct them (the "this" in your second code snippet).

Similar Threads

  1. basic C++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 24th August 2010, 08:29
  2. very basic Qt/c++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 2
    Last Post: 25th July 2010, 14:02
  3. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 07:46
  4. Basic question
    By giacomelli.fabio in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 00:12
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

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.