Results 1 to 5 of 5

Thread: Connecting different GUI sections

  1. #1
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Connecting different GUI sections

    I'm currently learning Qt/C++, and trying to write an app which consists of the following widget structure:

    A (main window)
    - B (left panel)
    --- C (multiple widgets based on QLabel - allows user to mock up an optical circuit)
    - D (right panel)
    --- E (line edit boxes on form, containing details of currently selected 'C')

    When the user clicks on a particular instance of widget C, I'd like to update the various form elements in E with the details stored within the selected widget (e.g. ID code). What's the best way to connect the two GUI regions together to achieve this?

    So far I've tried the following:

    [1] - Signals/Slots: I've put the following into B:
    Qt Code:
    1. connect(instanceOfC, SIGNAL(clicked(int ID)), parentWidget(), SLOT(updateID(int ID)));
    To copy to clipboard, switch view to plain text mode 
    Widget A contains the updateID() method, which contains the following code:
    Qt Code:
    1. instanceOfD->instanceofE->setText(...);
    To copy to clipboard, switch view to plain text mode 
    When this line is executed, the application produces a segmentation fault.

    [2] - I've put an eventFilter in A to catch MouseButtonPress events in C. I then get the same segmentation fault described above when I try to update the fields in E, and I also don't know how to transfer parameters stored in C this way.

    I hope this makes some sense. Any suggestions would be greatly appreciated!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting different GUI sections

    First ,
    connect(instanceOfC, SIGNAL(clicked(int ID)), parentWidget(), SLOT(updateID(int ID)));
    You dont have to pass argument name in SIGNAL and SLOT, only the signature.. Like -

    connect(instanceOfC, SIGNAL(clicked(int )), parentWidget(), SLOT(updateID(int )));
    Second, some code would help us identify the problem better.

    Third, if C is QLabel, does it have clicked() signal ? I guess clicked() is for QPushButton.

  3. The following user says thank you to aamer4yu for this useful post:

    ajb (13th May 2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting different GUI sections

    in your C class re implement your mousepress event and emit your own signal as
    emit setValue("IP"); which is setValue(const QString &) ..

    in your E class connect
    Qt Code:
    1. (C, SIGNAL(setValue(const QString &)), this, SLOT(getValue(const QString &)));
    To copy to clipboard, switch view to plain text mode 
    in slot of E class
    Qt Code:
    1. ::getValue(const QString &message)
    2. setText(message);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wagmare for this useful post:

    ajb (13th May 2009)

  6. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting different GUI sections

    if you have seg fault here:
    Qt Code:
    1. instanceOfD->instanceofE->setText(...);
    To copy to clipboard, switch view to plain text mode 
    that means that one of those pointers: "instanceOfD" or "instanceofE" is invalid, maybe you forgot about creating object with new or forgot that you have deleted it with delete. Pasting your code here would be helpful in finding the problem. Another thing is that debugger is the right tool to investigate such problems, so you can give it a try :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. The following user says thank you to faldzip for this useful post:

    ajb (13th May 2009)

  8. #5
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting different GUI sections

    Thanks very much for the advice! The segmentation faults were because I was accidentally reinitialising my objects in the constructor after defining them in the header - that's now fixed - thanks faldżip.

    I've got it working now so that the code passes messages from C to E. However the current implementation is pretty inefficient, because I have to set up new connections for each new instance of C (the Mirror class). I've done it this way because I'm not sure how objects talk to each other - this way, B creates lots of C, and can communicate back to A which knows all about D and E, so it seemed the easiest place to set up the connections.

    The relevant parts of the code are shown below. I like the idea of what wagmare suggested, i.e. putting the connection code into D/E to listen to any signals from C. I don't know how to implement this though, as I'm not sure how to tell D/E that C exists, and how to listen to any instance of C.

    Here's the relevant code:

    A (MainView - instantiated once)
    - B (DesignView - instantiated once)
    --- C (Mirror - lots of these, created by user - the main part of the app)
    - D (PropertyView - instantiated once)
    --- E (line edit boxes on form)

    The idea of the app, a tool for designing optical mirror/lens arrangements, is that the user sets up as many instances of C as they want, links them together in a user-defined order, and can click on any instance of C to reveal their properties in E.

    [A] MainView.h
    Qt Code:
    1. class MainView : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MainView(QWidget *parent = 0);
    7.  
    8. public slots:
    9. void changeID(const QString &);
    10.  
    11. private:
    12. PropertyView *propertypane;
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    [A] Mainview.cpp
    Qt Code:
    1. MainView::MainView(QWidget *parent) : QWidget(parent)
    2. {
    3. QHBoxLayout *mainLayout = new QHBoxLayout;
    4. DesignView *designpane = new DesignView(this);
    5. propertypane = new PropertyView(this);
    6. mainLayout->addWidget(designpane);
    7. mainLayout->addWidget(propertypane);
    8. setLayout(mainLayout);
    9. }
    10.  
    11. void MainView::changeID(const QString &message)
    12. {
    13. propertypane->setID(message);
    14. }
    To copy to clipboard, switch view to plain text mode 

    [B] DesignView.h
    Qt Code:
    1. class DesignView : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DesignView(QWidget *parent = 0);
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 

    [B] DesignView.cpp
    Qt Code:
    1. DesignView::DesignView(QWidget *parent) : QWidget(parent)
    2. {
    3. QGridLayout *grid = new QGridLayout;
    4. Mirror *firstMirror = new Mirror(this,0,1);
    5. grid->addWidget(firstMirror,1,0);
    6. setLayout(grid);
    7.  
    8. // have to do this for each mirror if defined here - not ideal...
    9. connect(firstMirror, SIGNAL(setID(const QString &)), parentWidget(), SLOT(changeID(const QString &)) );
    10. }
    To copy to clipboard, switch view to plain text mode 

    [C] Mirror.h
    Qt Code:
    1. class Mirror : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Mirror(QWidget *parent = 0);
    7.  
    8. signals:
    9. void setID(const QString &);
    10.  
    11. private:
    12. void mousePressEvent(QMouseEvent* event);
    13. int intID;
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    [C] Mirror.cpp
    Qt Code:
    1. Mirror::Mirror(QWidget *parent) : QLabel(parent)
    2. {
    3. // code to set up unique ID number
    4. intID = ...;
    5. }
    6.  
    7. void Mirror::mousePressEvent(QMouseEvent *event)
    8. {
    9. if ( event->buttons() == Qt::LeftButton )
    10. {
    11. // code to convert int to QString
    12. QVariant v = intID;
    13. strID = v.toString();
    14. emit setID(strID);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    [D] PropertyView.h
    Qt Code:
    1. class PropertyView : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PropertyView(QWidget *parent = 0);
    7. void setID(QString strTest);
    8.  
    9. private:
    10. QFormLayout *formLayout;
    11. QLineEdit *idLineEdit;
    12.  
    13. };
    To copy to clipboard, switch view to plain text mode 

    [D] PropertyView.cpp
    Qt Code:
    1. PropertyView::PropertyView(QWidget *parent) : QWidget(parent)
    2. {
    3. formLayout = new QFormLayout;
    4. idLineEdit = new QLineEdit;
    5. formLayout->addRow("ID:", idLineEdit);
    6. setLayout(formLayout);
    7. }
    8.  
    9. void PropertyView::setID(QString strID)
    10. {
    11. idLineEdit->setText(strID);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Finally [E] is the QLineEdit instance (idLineEdit).

    Any thoughts? Thanks again for the advice so far. : )
    Last edited by ajb; 13th May 2009 at 13:53.

Similar Threads

  1. dynamic signal connecting
    By donglebob in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2008, 09:58
  2. Replies: 4
    Last Post: 16th November 2008, 13:53
  3. Replies: 15
    Last Post: 6th April 2008, 10:06
  4. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 12:40
  5. QSocket: connecting but not connected
    By olberg in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2006, 10:26

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
  •  
Qt is a trademark of The Qt Company.