Results 1 to 9 of 9

Thread: Problem with comunication between widgets

  1. #1
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with comunication between widgets

    Hi
    I'm trying to create very simple (training) program for viewing pictures. The idea is to create application with one window that contains one button and another widget to paint the picture on it. So when i click the button image appears in designated area. Now, I have created class "paintarea" then I put QWidget on the form and promoted that widget to "paintarea". And that is where I got stuck. I don't know how to connect the QPushButton that is on the QMAinWIndow "form", with that promoted widget, so that if I click the button, then the signal sends a message to custom widget with fileName to be loaded ?? I hope I didn't confuse You to much. Below is my code of this app. All help will be very appreciated. Sorry for poor english.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QPushButton>
    6.  
    7. namespace Ui
    8. {
    9. class mainwindow;
    10. }
    11.  
    12. class mainwindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. mainwindow(QWidget *parent = 0);
    18. ~mainwindow();
    19.  
    20. private:
    21. Ui::mainwindow *ui;
    22. QPushButton *button;
    23.  
    24. public slots:
    25. void send_signal();
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. mainwindow::mainwindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::mainwindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. button = new QPushButton;
    10. button = ui->button;
    11.  
    12. connect (button,SIGNAL (clicked()),this,SLOT (send_signal()));
    13. }
    14.  
    15. mainwindow::~mainwindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void mainwindow::send_signal()
    21. {
    22. paint_space->load_image("/home/xxx/picture.jpeg");
    23. }
    To copy to clipboard, switch view to plain text mode 

    paintarea.h
    Qt Code:
    1. #ifndef PAINTAREA_H
    2. #define PAINTAREA_H
    3.  
    4. #include <QWidget>
    5. #include <QPainter>
    6. #include <QPixmap>
    7.  
    8. class paintarea : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. paintarea(QWidget *parent = 0);
    13. void load_image(const QString &fileName);
    14.  
    15. protected:
    16. void paintEvent(QPaintEvent *event);
    17. QPixmap image;
    18. bool status;
    19. };
    20.  
    21. #endif // PAINTAREA_H
    To copy to clipboard, switch view to plain text mode 

    paintarea.cpp

    Qt Code:
    1. #include "paintarea.h"
    2.  
    3. paintarea::paintarea (QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. //QPixmap image;
    7. // image = new QPixmap;
    8. status = false ;
    9. }
    10.  
    11. void paintarea::paintEvent(QPaintEvent *event)
    12. {
    13. if(status)
    14. {
    15. QPainter painter(this);
    16. painter.drawPixmap(0,0,image);
    17. //painter.drawRect(0,0,30,40);
    18.  
    19. }
    20.  
    21. }
    22.  
    23. void paintarea::load_image(const QString &fileName)
    24. {
    25.  
    26. image.load(fileName);
    27. status = true ;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with comunication between widgets

    you don't have to create a new button since the ui loader does that for you. You can access it as you do via ui->button. So just use that in your connect statement instead your temporary pointer:
    Qt Code:
    1. connect(ui->button, SIGNAL/*...*/);
    To copy to clipboard, switch view to plain text mode 
    And for simply displaying an image, you can use QLabel.

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

    laabor (3rd May 2010)

  4. #3
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with comunication between widgets

    Thanks for help, but now I have new problem. I have added two QLabels, one on my QMainWindow widget and second on "paintArea" widget. Now what I want to do is change text on both of them, but from within "paintArea.h" class. I have tried quite a few things, but can't get this to work ?? A bit of code eplaining what i wabt to acomplish:
    Qt Code:
    1. #include "paintarea.h"
    2.  
    3. paintarea::paintarea (QWidget *parent) :
    4. QWidget(parent)
    5. {
    6.  
    7. status = false ;
    8.  
    9. }
    10.  
    11. void paintarea::paintEvent(QPaintEvent *event)
    12. {
    13. if(status)
    14. {
    15. QPainter painter(this);
    16. painter.drawPixmap(0,0,image);
    17.  
    18.  
    19. }
    20.  
    21. }
    22.  
    23. void paintarea::load_image(const QString &fileName)
    24. {
    25.  
    26.  
    27. image.load(fileName);
    28.  
    29. status = true ;
    30. update();
    31. // this->label->setText(image.width()); <---- that doesn't work, but this is example what i want to do, and I know I need first convert "image.width()" to string
    32. //ui->label_2->setText(image.height()); <---- also not woking
    33. }
    To copy to clipboard, switch view to plain text mode 

    I know I need to declare somehow this "ui" but not sure exactly how, where ?

  5. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with comunication between widgets

    In paintarea, if image load suceed emit signal, connect that signal from mainwindows. Each time that signal is emited slot (in mainwindow) will fire up, something like this:
    mainwindow:
    Qt Code:
    1. connect( pArea, SIGNAL(mySIGNAL(int)), this, SLOT(updateLABEL(int)));
    To copy to clipboard, switch view to plain text mode 
    paintarea must be on the stack i.e. paintarea *pArea; (include paintarea.h in mainWindow.h)

    in mainWindow.cpp add
    Qt Code:
    1. private slots:
    2. void updateLABEL(int);
    To copy to clipboard, switch view to plain text mode 
    and:
    Qt Code:
    1. void MainWindow::updateLABEL(int w)
    2. {
    3. //update label text
    4. this->ui->label->setText( QVariant(w).toString() );
    5. }
    To copy to clipboard, switch view to plain text mode 

    int paintarea.cpp:

    Qt Code:
    1. void paintarea::load_image(const QString &fileName)
    2. {
    3.  
    4.  
    5. image.load(fileName);
    6.  
    7. status = true ;
    8. update();
    9. // this->label->setText(image.width()); <---- that doesn't work, but this is example what i want to do, and I know I need first convert "image.width()" to string
    10. //ui->label_2->setText(image.height()); <---- also not woking
    11.  
    12. //emit signal if image load suceed
    13. emit mySIGNAL( image.width());
    14. }
    To copy to clipboard, switch view to plain text mode 

    int paintarea.h:
    Qt Code:
    1. signals:
    2. void mySIGNAL( int );
    To copy to clipboard, switch view to plain text mode 

    Best luck

  6. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Problem with comunication between widgets

    Why does it not work?
    It's nice to be important but it's more important to be nice.

  7. #6
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with comunication between widgets

    I've made changes recommended by Talei, but still no luck all I can get is "Program finished unexpectedly, exited with code 0". There is no other errors in compilation. Any ideas why that's no working ? new code :
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QPushButton>
    6. #include <paintarea.h>
    7.  
    8. namespace Ui
    9. {
    10. class mainwindow;
    11. }
    12.  
    13. class mainwindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. mainwindow(QWidget *parent = 0);
    19. ~mainwindow();
    20.  
    21.  
    22.  
    23. private:
    24. Ui::mainwindow *ui;
    25. QPushButton *button;
    26. paintarea *parea;
    27.  
    28. public slots:
    29. void send_signal();
    30.  
    31.  
    32. private slots:
    33. void updateLabel(int);
    34.  
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. mainwindow::mainwindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::mainwindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. button = new QPushButton;
    10. button = ui->button;
    11.  
    12. paintarea *parea;
    13.  
    14. connect (button,SIGNAL (clicked()),this,SLOT (send_signal()));
    15. connect (parea, SIGNAL(mySignal(int)), this, SLOT(updateLabel(int)));
    16. }
    17.  
    18.  
    19. mainwindow::~mainwindow()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void mainwindow::send_signal()
    25. {
    26. ui->paint_space->load_image("/home/laabor/Pictures/test.jpeg");
    27. }
    28.  
    29. void mainwindow::updateLabel(int w)
    30. {
    31. this->ui->label->setText(QVariant(w).toString());
    32. }
    To copy to clipboard, switch view to plain text mode 
    paintarea.h
    Qt Code:
    1. #ifndef PAINTAREA_H
    2. #define PAINTAREA_H
    3.  
    4. #include <QWidget>
    5. #include <QPainter>
    6. #include <QPixmap>
    7. #include <QLabel>
    8.  
    9. class paintarea : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. paintarea(QWidget *parent = 0);
    14. void load_image(const QString &fileName);
    15.  
    16. protected:
    17. void paintEvent(QPaintEvent *event);
    18. QPixmap image;
    19. QLabel label;
    20. bool status;
    21.  
    22. signals:
    23. void mySignal(int);
    24. };
    25.  
    26. #endif // PAINTAREA_H
    To copy to clipboard, switch view to plain text mode 
    paintarea.cpp
    Qt Code:
    1. #include "paintarea.h"
    2.  
    3. paintarea::paintarea (QWidget *parent) :
    4. QWidget(parent)
    5. {
    6.  
    7. status = false ;
    8.  
    9.  
    10. }
    11.  
    12. void paintarea::paintEvent(QPaintEvent *event)
    13. {
    14. if(status)
    15. {
    16. QPainter painter(this);
    17. painter.drawPixmap(0,0,image);
    18.  
    19.  
    20. }
    21.  
    22. }
    23.  
    24. void paintarea::load_image(const QString &fileName)
    25. {
    26.  
    27.  
    28. image.load(fileName);
    29. status = true ;
    30. update();
    31.  
    32. emit mySignal(image.width());
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Problem with comunication between widgets

    The program very like crashes in line 15 of mainwindow.cpp because of access to unitialized variable parea.

    Also I do not understand what you want to do in line 9 & 10 of mainwindow.cpp.
    It's nice to be important but it's more important to be nice.

  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: Problem with comunication between widgets

    Some changes I would make to your code:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QPushButton>
    6. #include <paintarea.h>
    7.  
    8. namespace Ui
    9. {
    10. class mainwindow;
    11. }
    12.  
    13. class mainwindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. mainwindow(QWidget *parent = 0);
    19. ~mainwindow();
    20.  
    21.  
    22.  
    23. private:
    24. Ui::mainwindow *ui;
    25. //QPushButton *button; <---- Not necessary, ui->button is what you need.
    26. paintarea *parea;
    27.  
    28. public slots:
    29. void send_signal();
    30.  
    31.  
    32. private slots:
    33. void updateLabel(int);
    34.  
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. mainwindow::mainwindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::mainwindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. //button = new QPushButton;
    10. //button = ui->button; <--- Not needed, it adds no functionality.
    11.  
    12. paintarea *parea;
    13.  
    14. // connect (button,SIGNAL (clicked()),this,SLOT (send_signal()));
    15. connect(ui->button, SIGNAL(clicked()), this, SLOT(send_signal()));
    16. connect (parea, SIGNAL(mySignal(int)), this, SLOT(updateLabel(int)));
    17. }
    18.  
    19.  
    20. mainwindow::~mainwindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void mainwindow::send_signal()
    26. {
    27. ui->paint_space->load_image("/home/laabor/Pictures/test.jpeg");
    28. }
    29.  
    30. void mainwindow::updateLabel(int w)
    31. {
    32. //this->ui->label->setText(QVariant(w).toString()); <--- Incorrect! You have to define a QVariant first, or use static functions.
    33. ui->label->setText(QString::number(w));
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tbscope; 8th May 2010 at 12:31. Reason: copied code twice

  10. #9
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with comunication between widgets

    I'v found solution, it turned out to be very easy:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. mainwindow::mainwindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::mainwindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9.  
    10.  
    11.  
    12. connect (ui->button,SIGNAL (clicked()),this,SLOT (send_signal()));
    13. connect (ui->paint_space, SIGNAL(mySignal(int)), this, SLOT(updateLabel(int)));
    14. }
    15.  
    16.  
    17. mainwindow::~mainwindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void mainwindow::send_signal()
    23.  
    24. {
    25. QString fileName = QFileDialog::getOpenFileName(this);
    26. ui->paint_space->load_image(fileName);
    27.  
    28.  
    29. }
    30.  
    31. void mainwindow::updateLabel(int w)
    32. {
    33. ui->label->setText("poszlo");
    34. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Problem of including Widgets into DLL
    By dsedov in forum Qt Programming
    Replies: 15
    Last Post: 14th July 2021, 23:05
  2. Problem accesing widgets
    By Greender in forum Qt Programming
    Replies: 4
    Last Post: 6th February 2009, 12:14
  3. Problem in using Custom Widgets
    By vjn in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 13th April 2008, 21:40
  4. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  5. Dock widgets PROBLEM
    By L1L0 in forum Qt Programming
    Replies: 8
    Last Post: 7th April 2006, 18:29

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.