Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: A Qt4 program problem I can't make to work

  1. #1
    Join Date
    Jan 2007
    Beans
    248

    A Qt4 program problem I can't make to work

    Hi,
    I'm trying to make a QListWidget and
    1)--------------------------------
    or
    2) connect all the items on one slot and somehow identify witch item
    was double clicked ....
    Please help me I'm trying to do this for 2 days now ...
    here is my code:
    Code:
    #include <QApplication>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QListWidget>
    #include <QListWidgetItem>
    int main(int argc, char *argv[]) 
    {
    QApplication app(argc, argv); 
    QWidget *window = new QWidget; 
    QLabel *label = new QLabel("test");
    label->show();
    QListWidget *listWidget = new QListWidget;
    QListWidgetItem *Test = new QListWidgetItem("Test", listWidget);
    new QListWidgetItem("Test2", listWidget);
    new QListWidgetItem("Test3", listWidget);
    window-> setWindowTitle("Test list view");
    
    QObject::connect(listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),label , SLOT(hide()));
    
    QVBoxLayout *layout = new QVBoxLayout; 
        layout->addWidget(label);
        layout->addWidget(listWidget);
        window->setLayout(layout); 
        window->show(); 
    return app.exec();
    }
    This thing works, but hides the label no matter witch item is double clicked ... I'm asking you for the ultimate help ... add the code it needs to fulfill either 1) or 2) because if I think over this for another day I'll break something.
    Last edited by cherva; May 4th, 2008 at 10:18 AM.

  2. #2
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: A Qt4 program problem I can't make to work

    I'm an absolute beginner at Qt programming, so I'm probably using a sledgehammer to crack a walnut, here

    Here's my very ugly stab at a solution, which unfortunately involves the subclassing of both QListWidget and QListWidgetItem (*shudder*). Comments in the code should explain what's going on and how to use it.

    Here, only "Test" will call hide(). You'll need to connect other MyQListWidgetItems in the same way if you want them to hide(), too.

    Code:
    qmake -project && qmake && make
    to build!

    NOTE:

    The docs advise using QListView if you want more flexibility, so you might want to investigate this - Qt4's model/ view stuff is supposed to be pretty neat

  3. #3
    Join Date
    Feb 2007
    Location
    Seville, Spain
    Beans
    554
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: A Qt4 program problem I can't make to work

    Instead of connecting it to the label's hide slot, define a window class which will encapsulate all your window code (widget creation, etc) and there define a slot in which you check which element was clicked and hide the label accordingly.

  4. #4
    Join Date
    Jan 2007
    Beans
    248

    Re: A Qt4 program problem I can't make to work

    You saved two lives today. Mine because I was going crazy and my computer's life because I was going to kill him with a hammer THANKS A LOT
    Cherva bows infront of GeneralZod

    luisromangz thank you too

    Edit:
    Arrrrr I'm so stupid now when I see your code I realize that calling a different function won't help me because the menu items are dynamic and should not have a limit ... because I'm making a small Messenger and it will be stupid to have a limit of the users ... It's funny how I managed to make the chat function, but I can't make the users menu ... GeneralZod sorry for the wasted time I'll edit the first post so I'm waiting for solutions about 2). If no one help me I'll use the GeneralZod's code and somehow manage to make a nice program .
    P.S
    I'll include your name when the program has RC1, so what are you waiting for
    Last edited by cherva; May 4th, 2008 at 10:11 AM.

  5. #5
    Join Date
    Feb 2007
    Location
    Seville, Spain
    Beans
    554
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: A Qt4 program problem I can't make to work

    Ok but just be sure of not subclassing widget classes just for functionality like this, because that's not the easy nor the correct way.

  6. #6
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: A Qt4 program problem I can't make to work

    Quote Originally Posted by cherva View Post
    You saved two lives today. Mine because I was going crazy and my computer's life because I was going to kill him with a hammer THANKS A LOT
    Cherva bows infront of GeneralZod

    luisromangz thank you too

    Edit:
    Arrrrr I'm so stupid now when I see your code I realize that calling a different function won't help me because the menu items are dynamic and should not have a limit ... because I'm making a small Messenger and it will be stupid to have a limit of the users ... It's funny how I managed to make the chat function, but I can't make the users menu ... GeneralZod sorry for the wasted time I'll edit the first post so I'm waiting for solutions about 2). If no one help me I'll use the GeneralZod's code and somehow manage to make a nice program .
    P.S
    I'll include your name when the program has RC1, so what are you waiting for
    Heh - I think you're going to have to explain a little more about what you're doing, here - I'm not sure why this approach would not be dynamic nor why it would impose limits on the number of users, and Luis's suggestion fulfils #2 quite admirably.

    What do the items in the list represent, and what distinct behaviours do you want when different list elements are double-clicked?

  7. #7
    Join Date
    Feb 2007
    Location
    Seville, Spain
    Beans
    554
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: A Qt4 program problem I can't make to work

    Hey mate, the signal launched when the item is double-clicked (itemDoubleClicked(QListWidgetItem*)) passes the clicked item as a parameter, provided your slot uses it.

    So in your generic slot for all items you just have to check if the passed item should be hidden.

  8. #8
    Join Date
    Jan 2007
    Beans
    248

    Re: A Qt4 program problem I can't make to work

    The items in the list reprisent the users in your user list they will be read from a file ... after you double click one the program will search the file for the IP of the user and start the chat program with him.
    Hey mate, the signal launched when the item is double-clicked (itemDoubleClicked(QListWidgetItem*)) passes the clicked item as a parameter, provided your slot uses it.

    So in your generic slot for all items you just have to check if the passed item should be hidden.
    Yes I know that, but I don't know hot to do it can you show me an example code ?
    Last edited by cherva; May 4th, 2008 at 01:29 PM.

  9. #9
    Join Date
    Feb 2007
    Location
    Seville, Spain
    Beans
    554
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: A Qt4 program problem I can't make to work

    Ok, let's see.

    In your ContactListWindow (as you should be subclassing Window as I wrote in a previous post) class header file you should define a non public slot in this way:

    Code:
    protected slots:
      /*$PROTECTED_SLOTS$*/
      void onItemDoubleClicked(QListWidgetItem * item );
    Then in the class' implementation file, you should write a body for that slot:

    Code:
    void ContactListWindow::onItemDoubleClicked(QListWidgetItem * item)
    {
      if(item->text() == "Paco")
        item->setHidden(true);
    }
    And you should link the listwidget's doubleclick signal with the slot using

    Code:
    QObject::connect(listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
                       this, SLOT(onItemDoubleClicked(QListWidgetItem * )));
    for exaple in the ContactListWindow's constructor method.

    Maybe you should consider purchasing a book as "C++ GUI Programming with Qt 4", which covers from begginer's problems like this and goes into far more complex problems and have a lot of code examples.
    Last edited by luisromangz; May 4th, 2008 at 02:16 PM.

  10. #10
    Join Date
    Jan 2007
    Beans
    248

    Re: A Qt4 program problem I can't make to work

    Thanks I'll test this right away
    Last edited by cherva; May 4th, 2008 at 03:29 PM.

Page 1 of 2 12 LastLast

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
  •