Results 1 to 11 of 11

Thread: Shopping list type of program: how to move bought items to the bottom of QTreeWidget?

  1. #1
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Symbian S60

    Default Shopping list type of program: how to move bought items to the bottom of QTreeWidget?

    Hi!

    I'm a beginner with Qt and programming in general, and I'm having a problem with a shopping list type of program I'm trying to do. I have attached a picture to show a bit of what I'm trying to execute..

    So I have a QTreeWidget, where I add the items, which are QWidgets. I add them with setCellWidget, when the "Add"-button is clicked. It creates a new widget into the QTreeWidget with the information set below it. I have gotten this to work fine, but I have no idea of the solutions, if there are any, to move the objects that have their checkboxes checked to the bottom of the QTreeWidget.

    I assume that I should somehow keep a list of the added items, and their palce in the list. And when the checkbox of an item is clicked, the widget would send a signal that would move the wanted item down and handle the other items and their places accordingly. I don't really have any idea how this could be done, I don't know how new signals and slots are created, even though I have read the documentations and tried to check the examples.

    Thank you very much in advance, maybe I could be of help for some newbie in the future, too !
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    What you are looking after is sorting.
    You want to sort the items in such a way that the unchecked items are on top and the checked items are at the bottom.

    Personal opinion:
    I don't like it when lists change when I click on an item in the list. I would create two lists. One with all possible items I can buy and one with items I did buy.

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    I don't know how new signals and slots are created
    Well, you instantiate a widget that can emit at least one signal (Qt's classes often include one or more signals, which you can find in the class documentation), and then you call QObject::connect() to associate that signal firing with some kind of consequence (usually a function call). Calls to connect() often happen inside a constructor method. A QPushButton quite naturally has a "clicked" signal for example, and we can make use of that signal like this:
    Qt Code:
    1. QObject::connect(yourButton, SIGNAL(clicked()), someReceiverObject, SLOT(onButtonClicked()));
    To copy to clipboard, switch view to plain text mode 
    Now, whenever the button in question is clicked, someReceiverObject's onButtonClicked() function will be called. The onButtonClicked() function is like any other function except that it has been declared as a slot in the appropriate header file:
    Qt Code:
    1. ...
    2. public slots:
    3. void onButtonClicked(); // just an example, doesn't have to be void
    4. ...
    To copy to clipboard, switch view to plain text mode 

    I suggest reading and re-reading the documentation on signals and slots until you understand most of it. It might take some time to sink in. It might be an idea to hammer away at an introductory text or online tutorial for Object Oriented Programming concepts, too.

  4. #4
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Symbian S60

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Thank you to you both !

    tbscope: I know that I need some kind of sorting, it's just that I don't know how to execute that kind of behaviour in this kind of situation. I bet it needs much work to get this program doing it :S?

    Urthas: I know what signals and slots are, and I know how to use them and all that, but I don't know how to create a new signal for an object to emit. I don't remember seeing how to do that in the documentations, and didn't really even find it now when I re-checked them.

    I'm really glad for your replies .

  5. #5
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Quote Originally Posted by weeezes View Post
    I don't know how to create a new signal for an object to emit.
    Nothing easier! In your header file, include the following section:

    Qt Code:
    1. signals:
    2. void yourSignal(); // can also have parameters
    3. ...
    4. //rest of class
    To copy to clipboard, switch view to plain text mode 

    That's it! (Well ok, they can be declared as protected etc., but that can come later). There is no "definition" of signals in the .cpp file, and their return type must be void. Then, in the .cpp file, you will have the following:

    Qt Code:
    1. retType yourClass::yourFunction(yourParams)
    2. {
    3. ...
    4. emit yourSignal();
    5. ...
    6. return ...;
    7. }
    To copy to clipboard, switch view to plain text mode 

    The emit statement is often located near or at the end of a function. So now an object of type yourClass will emit yourSignal() whenever yourFunction() is called, and that object can be meaningfully connected to a slot.

    Hope this helps!

  6. The following user says thank you to Urthas for this useful post:

    weeezes (29th August 2010)

  7. #6
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Symbian S60

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Wow, thank you very much, Urthas! That's exactly what I was strugling to find! That seems so simple that I almost feel embarassed that I ever asked about it . The emit statement is a new thing for me, maybe it's a so short word that I have missed it completely . Let's see if I get something done with this new skill of mine >...

    Thank you again .

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Quote Originally Posted by tbscope View Post
    What you are looking after is sorting.
    You want to sort the items in such a way that the unchecked items are on top and the checked items are at the bottom.
    As far as I remember this won't make the cell widgets move as they are not tied to the model in any way Unless the Trolls changed this behaviour within the last two years or so...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Ohh, I didn't know that.

    Sorting would have been the first thing I would try though.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    And I would go for persistant editors instead of cell widgets and moving the items to the bottom of the list manually. Sorting would not be that reliable here as it would be difficult to decide how to order two items with the same check status. Of course such program will only work with lists no longer than about 10 items. For longer ones it'd get very unresponsive due to use of cell widgets or persistant editors.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Symbian S60

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Oh, now I'm confused . What do you mean by "persistant editors"?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Shopping list type of program: how to move bought items to the bottom of QTreeWid

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Move items up and down in QListWidget
    By araglin in forum Newbie
    Replies: 7
    Last Post: 31st August 2016, 12:05
  2. List all elements of a QTreeWidget from top to bottom
    By ricardosf in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2010, 03:46
  3. getting a file extenstion list from a MIME type
    By roxton in forum Qt Programming
    Replies: 0
    Last Post: 10th April 2009, 19:27
  4. Replies: 1
    Last Post: 22nd November 2008, 07:32
  5. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 10:40

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.