Results 1 to 18 of 18

Thread: Help with UI Widgets

  1. #1
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Help with UI Widgets

    Sup guys.

    I'm trying to set a value for my button of another file in this print is in the main but my intention is to put in the socket.cpp.

    The error that is happening is that my program crashes. Thanks

    http://i.cubeupload.com/4OmfAJ.png <- pic from crashes.

    Code:
    Qt Code:
    1. #include "socket.h"
    2. #include "mainwindow.h"
    3. #include "ui_mainwindow.h"
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. Socket s;
    10. MainWindow w;
    11. w.show();
    12. w.setWindowTitle("olea");
    13. Ui::MainWindow *abcde;
    14. abcde->btnEnviar->setText("olea");
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    Quote Originally Posted by ReDKiiL View Post
    Sup guys.

    I'm trying to set a value for my button of another file in this print is in the main but my intention is to put in the socket.cpp.

    The error that is happening is that my program crashes. Thanks

    http://i.cubeupload.com/4OmfAJ.png <- pic from crashes.
    The compiler warning points directly at your problem. abcde is an uninitialized pointer variable in line 13 and when you execute line 14, the contents of abcde are unknown.

  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: Help with UI Widgets

    You never initialise the abcde pointer, which is why the program crashes. The compiler has even warned you of this.

    What you are trying to do will not work in the way you expect even of you fix the pointer though

  4. #4
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Quote Originally Posted by ChrisW67 View Post
    You never initialise the abcde pointer, which is why the program crashes. The compiler has even warned you of this.

    What you are trying to do will not work in the way you expect even of you fix the pointer though
    Could you give me help on how to do it the right way?
    If it's not too much to ask

  5. #5
    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: Help with UI Widgets

    What you are doing creates a second instance of the generated Ui class that is not related to the one inside the main object. You can change these widgets without affecting the widgets that are visible in the main window.

    The main window class has a private member variable, ui, pointing at the objects representing the widgets on screen. For code external to the main window class to access the UI you need to provide a public method in the MainWindow class to allow them to do that.

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

    ReDKiiL (28th March 2015)

  7. #6
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Quote Originally Posted by ChrisW67 View Post
    What you are doing creates a second instance of the generated Ui class that is not related to the one inside the main object. You can change these widgets without affecting the widgets that are visible in the main window.

    The main window class has a private member variable, ui, pointing at the objects representing the widgets on screen. For code external to the main window class to access the UI you need to provide a public method in the MainWindow class to allow them to do that.
    As I write this method? could you give me an example?

    #edit
    I created a function in the mainwindow class to set the value of the button, and I called him on the socket.
    But it does not work, I think it's because the constructor of mainwindow is called 2 times.
    How can I call the mainwindow functions without calling the constructor?
    Last edited by ReDKiiL; 27th March 2015 at 21:51.

  8. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    Look at your MainWindow.cpp and MainWindow.h files. Assuming you created your User Interface (UI) using Qt Designer, then you will have a private variable named ui inside your MainWindow class. You would then use syntax similar to the following to use your various components for your UI:

    Qt Code:
    1. ui->setupUi(this); // after this statement you can address your various ui components like shown below
    2. ui->btnEnviar->setText("olea");
    To copy to clipboard, switch view to plain text mode 

    Remove the abcde stuff from your main() routine and place in the constructor for your MainWindow.

    Edit: Also, whatever you were planning on doing with your Socket class in the main() routine should also be moved into your MainWindow class or a different class, but certainly moved out of main().

  9. #8
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Quote Originally Posted by jthomps View Post
    Look at your MainWindow.cpp and MainWindow.h files. Assuming you created your User Interface (UI) using Qt Designer, then you will have a private variable named ui inside your MainWindow class. You would then use syntax similar to the following to use your various components for your UI:

    Qt Code:
    1. ui->setupUi(this); // after this statement you can address your various ui components like shown below
    2. ui->btnEnviar->setText("olea");
    To copy to clipboard, switch view to plain text mode 

    Remove the abcde stuff from your main() routine and place in the constructor for your MainWindow.
    Oh that's I need to change the value of the button from another class. Because this class will receive data from there depending on what is received that this button will change.

  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: Help with UI Widgets

    Please do not confuse the terms "class" and "object".
    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. The following user says thank you to wysota for this useful post:

    jefftee (27th March 2015)

  12. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    If you want to change the value of the button from another class, then use signals/slots and emit a signal from your other class, something like this in your other class header file:

    Qt Code:
    1. signals:
    2. void updateButton(const QString &text);
    To copy to clipboard, switch view to plain text mode 

    And then a slot in your MainWindow.h:

    Qt Code:
    1. public slots:
    2. void onUpdateButton(const QString &text);
    To copy to clipboard, switch view to plain text mode 

    And finally the slot itself:

    Qt Code:
    1. void MainWindow::onUpdateButton(const QString &text)
    2. {
    3. ui->btnEnviar->setText(text);
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Once you have added the signal and slot, connect them together so that when your other class wants to update the button text, it emits signal updateButton and your onUpdateButton slot will be invoked to update the button, etc.

  13. #11
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Quote Originally Posted by jthomps View Post
    If you want to change the value of the button from another class, then use signals/slots and emit a signal from your other class, something like this in your other class header file:

    Qt Code:
    1. signals:
    2. void updateButton(const QString &text);
    To copy to clipboard, switch view to plain text mode 

    And then a slot in your MainWindow.h:

    Qt Code:
    1. public slots:
    2. void onUpdateButton(const QString &text);
    To copy to clipboard, switch view to plain text mode 

    And finally the slot itself:

    Qt Code:
    1. void MainWindow::onUpdateButton(const QString &text)
    2. {
    3. ui->btnEnviar->setText(text);
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Once you have added the signal and slot, connect them together so that when your other class wants to update the button text, it emits signal updateButton and your onUpdateButton slot will be invoked to update the button, etc.
    Another help, connect has be placed always on the class of the signal?

  14. #12
    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: Help with UI Widgets

    Quote Originally Posted by jthomps View Post
    (...) when your other class wants to update the button text, it emits signal (...)
    Class emits a signal?
    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.


  15. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    Quote Originally Posted by ReDKiiL View Post
    Another help, connect has be placed always on the class of the signal?
    You really need to do some reading before you have a chance at succeeding to write any code. Create a member variable in MainWindow for your Socket. I would add one of the following in the constructor of your MainWindow:

    If you have added your member variable as a pointer to a Socket instance named pSocket for example, then use:

    Qt Code:
    1. connect(pSocket, &Socket:updateButton, this, &MainWindow::onUpdateButton);
    To copy to clipboard, switch view to plain text mode 

    If not a pointer but an actual instance named socket, then use:

    Qt Code:
    1. connect(&socket, &Socket:updateButton, this, &MainWindow::onUpdateButton);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by wysota View Post
    Class emits a signal?
    When an instance of your other class wants to update the button text, emit a signal...

    Touche

    Edit: Not sure what I did to combine my reply to wysota with my other reply, but it's done and I'm not going to fix it up!
    Last edited by jefftee; 27th March 2015 at 23:21.

  16. #14
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    I tried like this so nothing happens.

    Qt Code:
    1. //socket.h
    2. #ifndef SOCKET_H
    3. #define SOCKET_H
    4.  
    5. #include <QObject>
    6. #include <QDebug>
    7.  
    8. class Socket : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Socket(QObject *parent = 0);
    13. ~Socket();
    14.  
    15. signals:
    16. void enviarSinal(QString z);
    17. public slots:
    18. };
    19.  
    20. #endif // SOCKET_H
    21. //socket.cpp
    22. #include "socket.h"
    23.  
    24. Socket::Socket(QObject *parent) : QObject(parent)
    25. {
    26. qDebug() << "Socket iniciado!!!";
    27. emit enviarSinal("Get down");
    28. }
    29.  
    30. Socket::~Socket()
    31. {
    32.  
    33. }
    34. //principal.h (MainWindow)
    35. #ifndef PRINCIPAL_H
    36. #define PRINCIPAL_H
    37.  
    38. #include <QMainWindow>
    39. #include <QDebug>
    40.  
    41. namespace Ui {
    42. class Principal;
    43. }
    44.  
    45. class Principal : public QMainWindow
    46. {
    47. Q_OBJECT
    48.  
    49. public:
    50. explicit Principal(QWidget *parent = 0);
    51. ~Principal();
    52.  
    53. private:
    54. Ui::Principal *ui;
    55. public slots:
    56. void receberSinal(QString b);
    57. };
    58.  
    59. #endif // PRINCIPAL_H
    60.  
    61. //principal.cpp (MainWindow)
    62. #include "principal.h"
    63. #include "ui_principal.h"
    64. #include "socket.h"
    65.  
    66. Principal::Principal(QWidget *parent) :
    67. QMainWindow(parent),
    68. ui(new Ui::Principal)
    69. {
    70. Socket *s = new Socket();
    71. connect(s,SIGNAL(enviarSinal(QString)), this,SLOT(receberSinal(QString)));
    72. qDebug() << s;
    73. ui->setupUi(this);
    74. }
    75.  
    76. Principal::~Principal()
    77. {
    78. delete ui;
    79. }
    80. void Principal::receberSinal(QString b){
    81. qDebug() << "Recebendo Sinal:";
    82. qDebug() << b;
    83. qDebug() << "====SINAL RECEBIDO====";
    84. }
    To copy to clipboard, switch view to plain text mode 

    App output:
    http://prntscr.com/6m7wyy

  17. #15
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    Your Socket constructor runs and emits the signal before you have made the connection. I know you're just trying to test the signal/slot, but I presume in your actual implementation, you would emit the signal after you have received some data on the Socket, etc.

    If you want to just try a test, remove the emit from line 27 and add the following after line 73:

    Qt Code:
    1. emit s->enviarSinal("Get down");
    To copy to clipboard, switch view to plain text mode 

  18. The following user says thank you to jefftee for this useful post:

    ReDKiiL (28th March 2015)

  19. #16
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Thank you,Yes I am wanting to do some tests, in the future my code will be but complete.

  20. #17
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Help with UI Widgets

    So did that work?

  21. #18
    Join Date
    Mar 2015
    Posts
    18
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with UI Widgets

    Quote Originally Posted by jthomps View Post
    So did that work?
    Yep

    http://prntscr.com/6m8qsk

Similar Threads

  1. Replies: 4
    Last Post: 2nd February 2014, 06:35
  2. Replies: 0
    Last Post: 30th April 2012, 15:17
  3. Replies: 2
    Last Post: 30th March 2011, 20:20
  4. Replies: 2
    Last Post: 16th December 2010, 11:52
  5. Replies: 5
    Last Post: 18th April 2010, 23:31

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.