Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31

Thread: Q_OBJECT, vtables and basic GUI control.

  1. #21
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    If you want to replace the entire central widget of the QMainWindow with your tab widget then just call QMainWindow::setCentralWidget() (the existing central widget will be destroyed). If you intend to switch back and forth between several different central widgets then you should look at QStackedWidget.

    If that is not what you want than you should post a small compilable example that shows what you are doing and then explain what you want it to do.

  2. #22
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Well, I'm using QDialog, not QMainWindow, but yeah, it'll be easier to explain on code, here is it:

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include "LoginInterface.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. LoginInterface li;
    9. li.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    LoginInterface.cpp:
    Qt Code:
    1. #include "LoginInterface.h"
    2.  
    3. LoginInterface::LoginInterface(QWidget *parent, Qt::WindowFlags flags) :
    4. QDialog(parent, flags)
    5. {
    6. MainLayout = new QGridLayout;
    7.  
    8. // GUI Controls
    9. UserLabel = new QLabel(tr("User:"));
    10. UserInput = new QLineEdit;
    11.  
    12. PasswordLabel = new QLabel(tr("Password:"));
    13. PasswordInput = new QLineEdit;
    14.  
    15. LoginButton = new QPushButton(tr("Login"));
    16. ExitButton = new QPushButton(tr("Exit"));
    17.  
    18. connect(LoginButton, SIGNAL(clicked()), this, SLOT(Login()));
    19. connect(ExitButton, SIGNAL(clicked()), this, SLOT(Exit()));
    20.  
    21. // Adding Widgets
    22. MainLayout->addWidget(UserLabel);
    23. MainLayout->addWidget(UserInput);
    24.  
    25. MainLayout->addWidget(PasswordLabel);
    26. MainLayout->addWidget(PasswordInput);
    27.  
    28. MainLayout->addWidget(LoginButton);
    29. MainLayout->addWidget(ExitButton);
    30.  
    31. // Display
    32. setLayout(MainLayout);
    33. setWindowTitle("Login");
    34. }
    35.  
    36. void LoginInterface::Run()
    37. {
    38. // Totalny rozpierdol!
    39. delete UserLabel;
    40. delete UserInput;
    41.  
    42. delete PasswordLabel;
    43. delete PasswordInput;
    44.  
    45. delete LoginButton;
    46. delete ExitButton;
    47.  
    48. // Title
    49. data[1].clear();
    50. data[1] = "Logged as " + data[0];
    51. setWindowTitle(data[1]);
    52. data[1].clear();
    53.  
    54. // Basic Tab
    55. BasicWidget = new QWidget;
    56. BasicLayout = new QGridLayout;
    57.  
    58. // // Basic Tab - Kontrolki
    59. UsernameLabel = new QLabel("User:");
    60. UsernameEdit = new QLineEdit(data[0]);
    61. UsernameEdit->setReadOnly(true);
    62. data[0].clear();
    63.  
    64. // // Basic Tab - Dodawanie Widgetow
    65. BasicLayout->addWidget(UsernameLabel);
    66. BasicLayout->addWidget(UsernameEdit);
    67.  
    68. // // Basic Tab - Ustawianie Layoutu
    69. BasicWidget->setLayout(BasicLayout);
    70.  
    71. // Tabs
    72. Tabs = new QTabWidget;
    73. Tabs->addTab(BasicWidget, tr("Basic Info"));
    74. MainLayout->addWidget(Tabs);
    75. }
    76.  
    77. void LoginInterface::Login()
    78. {
    79. data[0] = UserInput->text();
    80. data[1] = PasswordInput->text();
    81.  
    82. Box->setWindowTitle("Login");
    83.  
    84. if(data[0] != "Diath" && data[1] != "test") // have to see QMySQLDriver later
    85. {
    86. Box->setText("You did enter wrong login data.");
    87. Box->setIcon(QMessageBox::Critical);
    88. Box->setStandardButtons(QMessageBox::Ok);
    89. }
    90. else
    91. {
    92. Box->setText("Welcome!");
    93. Box->setIcon(QMessageBox::Information);
    94. Box->setStandardButtons(QMessageBox::Ok);
    95.  
    96. Run();
    97. }
    98.  
    99. Box->exec();
    100. }
    101.  
    102. void LoginInterface::Exit()
    103. {
    104. exit(0);
    105. }
    To copy to clipboard, switch view to plain text mode 

    LoginInterface.h:
    Qt Code:
    1. #ifndef LOGININTERFACE_H
    2. #define LOGININTERFACE_H
    3.  
    4. #include <QtGui>
    5. #include <QDialog>
    6.  
    7. class LoginInterface : public QDialog
    8. {
    9. Q_OBJECT
    10. public:
    11. LoginInterface(QWidget *parent = 0, Qt::WindowFlags flags = 0);
    12. public slots:
    13. void Login();
    14. void Exit();
    15. private:
    16. void Run();
    17. QString data[2];
    18.  
    19. // Main Layout and its controls.
    20. QGridLayout *MainLayout;
    21. QLabel *UserLabel, *PasswordLabel;
    22. QLineEdit *UserInput, *PasswordInput;
    23. QPushButton *LoginButton, *ExitButton;
    24.  
    25. // Client Layouts and their controls.
    26. QGridLayout *BasicLayout;
    27. QWidget *BasicWidget;
    28. QTabWidget *Tabs;
    29.  
    30. // Basic Tab
    31. QLabel *UsernameLabel;
    32. QLineEdit *UsernameEdit;
    33. };
    34.  
    35. #endif // LOGININTERFACE_H
    To copy to clipboard, switch view to plain text mode 

    Now, I want to delete MainLayout while deleting old controls like UserInput, and then I have to replace this one:
    Qt Code:
    1. MainLayout->addWidget(Tabs);
    To copy to clipboard, switch view to plain text mode 

    With something that will set Tabs widget as parent windows "central widget", also I have to access somehow to parent window and set its size.

  3. #23
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    B.u.m.p.

  4. #24
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Your approach at the moment makes more work than you need. To apply a new layout in place of the old you need to delete the existing layout (see QWidget::setLayout() docs) and then apply the new one. That does not remove the original widgets (they are children of the QDialog not the layout) which you would then have to hide or delete yourself. See http://www.qtcentre.org/threads/3295...932#post152932

    I would use QStackedWidget. In the constructor set up your login user name and password layout on one page and the running stuff on the other and set the login page to be current. Then when they successfully log in switch the stacked layout current page. If there is a log out function then that just switches the pages back.
    Last edited by ChrisW67; 1st August 2010 at 23:20. Reason: updated contents

  5. #25
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Thanks for your advice, now I have such code so far:
    Qt Code:
    1. #include "LoginInterface.h"
    2.  
    3. LoginInterface::LoginInterface(QWidget *parent, Qt::WindowFlags flags) :
    4. QDialog(parent, flags)
    5. {
    6. // Main
    7. MainLayout = new QGridLayout;
    8.  
    9. // GUI Controls
    10. UserLabel = new QLabel(tr("User:"));
    11. UserInput = new QLineEdit;
    12.  
    13. PasswordLabel = new QLabel(tr("Password:"));
    14. PasswordInput = new QLineEdit;
    15. PasswordInput->setEchoMode(QLineEdit::Password);
    16.  
    17. LoginButton = new QPushButton(tr("Login"));
    18. ExitButton = new QPushButton(tr("Exit"));
    19.  
    20. connect(LoginButton, SIGNAL(clicked()), this, SLOT(Login()));
    21. connect(ExitButton, SIGNAL(clicked()), this, SLOT(Exit()));
    22.  
    23. // Adding Widgets
    24. MainLayout->addWidget(UserLabel);
    25. MainLayout->addWidget(UserInput);
    26.  
    27. MainLayout->addWidget(PasswordLabel);
    28. MainLayout->addWidget(PasswordInput);
    29.  
    30. MainLayout->addWidget(LoginButton);
    31. MainLayout->addWidget(ExitButton);
    32.  
    33. // Client
    34. ClientLayout = new QGridLayout;
    35. // Basic Tab
    36. BasicWidget = new QWidget;
    37. BasicLayout = new QGridLayout;
    38.  
    39. // // Basic Tab - Kontrolki
    40. UsernameLabel = new QLabel("User:");
    41. UsernameEdit = new QLineEdit();
    42. UsernameEdit->setReadOnly(true);
    43.  
    44. // // Basic Tab - Dodawanie Widgetow
    45. BasicLayout->addWidget(UsernameLabel);
    46. BasicLayout->addWidget(UsernameEdit);
    47.  
    48. // // Basic Tab - Ustawianie Layoutu
    49. BasicWidget->setLayout(BasicLayout);
    50.  
    51. // Tabs
    52. Tabs = new QTabWidget;
    53.  
    54. Tabs->addTab(BasicWidget, tr("Basic Info"));
    55.  
    56. // misc.
    57. ClientLayout->addWidget(Tabs);
    58.  
    59. setLayout(MainLayout);
    60. setWindowTitle("Login");
    61. }
    62.  
    63. void LoginInterface::Run()
    64. {
    65. delete UserInput;
    66. delete UserLabel;
    67. delete PasswordInput;
    68. delete PasswordLabel;
    69. delete LoginButton;
    70. delete ExitButton;
    71. delete MainLayout;
    72. // Title
    73. data[1].clear();
    74. data[1] = "Logged as " + data[0];
    75. QWidget::setWindowTitle(data[1]);
    76. data[1].clear();
    77.  
    78. UsernameEdit->insert(data[0]);
    79. data[0].clear();
    80.  
    81. // Layout
    82. QWidget::setFixedWidth(250);
    83. QWidget::setLayout(ClientLayout);
    84. }
    85.  
    86. void LoginInterface::Login()
    87. {
    88. data[0] = UserInput->text();
    89. data[1] = PasswordInput->text();
    90.  
    91. Box->setWindowTitle("Login");
    92.  
    93. if(data[0] != "Diath" && data[1] != "test")
    94. {
    95. Box->setText("You did enter wrong login data.");
    96. Box->setIcon(QMessageBox::Critical);
    97. Box->setStandardButtons(QMessageBox::Ok);
    98. }
    99. else
    100. {
    101. Box->setText("Welcome!");
    102. Box->setIcon(QMessageBox::Information);
    103. Box->setStandardButtons(QMessageBox::Ok);
    104.  
    105. Run();
    106. }
    107.  
    108. Box->exec();
    109. delete Box;
    110. }
    111.  
    112. void LoginInterface::Exit()
    113. {
    114. exit(0);
    115. }
    To copy to clipboard, switch view to plain text mode 

    But is there any way to resize QTabWidget like on following screen:

  6. #26
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Yes.
    You really should use Qt Assistant: QLayout::setContentsMargins()

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

    Diath (2nd August 2010)

  8. #27
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Finally, I have excepted result thank you very much .

  9. #28
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Oh, I'll probably annoy you guys, but you are much better at explaination and examples than documentation :x.

    So, I have file named "resource.rcc" in main project directory, with contents:
    Qt Code:
    1. <!DOCTYPE RCC>
    2. <RCC version="1.0">
    3. <qresource>
    4. <file>res/basic.ico</file>
    5. </qresource>
    6. </RCC>
    To copy to clipboard, switch view to plain text mode 

    Also, in main.cpp I have this above QApplication app()~:
    Qt Code:
    1. Q_INIT_RESOURCE(resource);
    To copy to clipboard, switch view to plain text mode 

    Also I have added file to project in Qt Creator, now I'm using following code to insert TAB (this code is in another file named LoginInterface.cpp):
    Qt Code:
    1. Tabs->addTab(BasicWidget, QIcon(":/res/basic.ico"), tr("Basic Info"));
    To copy to clipboard, switch view to plain text mode 

    But after compiling (CTRL+R) and running binary it doesn't seem to work (Just TAB width changed, but I don't see icon.).

    Any tips? (A)

  10. #29
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Add the resource file to the RESOURCES entry in your PRO file and dispense with the Q_INIT_RESOURCE() (unless you have a specific reason you require it).

    You might try ":res/basic.ico" as the file name, or make your QRC file read
    Qt Code:
    1. ...
    2. <qresource prefix="/">
    3. ...
    To copy to clipboard, switch view to plain text mode 

  11. #30
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Of course I have resource info in my pro file, it looks like:
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = client_launcher
    4. TEMPLATE = app
    5.  
    6. RESOURCES = resource.rcc
    7.  
    8. SOURCES += \
    9. src/main.cpp \
    10. src/LoginInterface.cpp
    11.  
    12. HEADERS += \
    13. src/LoginInterface.h
    14.  
    15. FORMS +=
    To copy to clipboard, switch view to plain text mode 

    I tried with ":/res/basic.ico", but then, Qt Creator simply doesn't compile code:
    Qt Code:
    1. :: error: [obj/qrc_resource.cpp] Error 1
    To copy to clipboard, switch view to plain text mode 

    Okay, I've removed Q_INIT_RESOURCE() but I don't think so it helped .

    Well, I have open Qt Resource System documentation, and saw this example:
    Qt Code:
    1. QResource::registerResource("/path/to/myresource.rcc");
    To copy to clipboard, switch view to plain text mode 

    So I've added in LoginInterface.cpp (in constructor) this:
    Qt Code:
    1. QResource::registerResource("../myresource.rcc");
    To copy to clipboard, switch view to plain text mode 

    (With 2 dots, because my resource file is in higher directory than source files.)
    But still I'm failing, any idea? :|

  12. #31
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Assuming a simple application without the complications of dynamically loaded plugins etc. you should not need do anything to register the resource file other than list it in the PRO file. No need to code anything, just use it.

    Quote Originally Posted by Diath View Post
    I tried with ":/res/basic.ico", but then, Qt Creator simply doesn't compile code:
    Qt Code:
    1. :: error: [obj/qrc_resource.cpp] Error 1
    To copy to clipboard, switch view to plain text mode 
    I wrote that you might try ":res/basic.ico" as the filename meaning in the cpp source where you load the QIcon, not in the QRC file. Note, there is no "/" between the ":" and the "res". Given that the example below works either way, I doubt this is your problem.

    Have you bothered to check if Windows ICO files are listed under QImageReader::supportedImageFormats()?

    Here is a complete example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow: public QMainWindow {
    4. public:
    5. MainWindow(QWidget *p = 0): QMainWindow(p) {
    6. QWidget *central = new QWidget(this);
    7. QLabel *widget1 = new QLabel(this);
    8. QPixmap pixmap1(":/res/stop.png");
    9. widget1->setPixmap(pixmap1);
    10. QLabel *widget2 = new QLabel(this);
    11. QPixmap pixmap2(":res/stop.png");
    12. widget2->setPixmap(pixmap2);
    13. QVBoxLayout *layout = new QVBoxLayout(central);
    14. layout->addWidget(widget1);
    15. layout->addWidget(widget2);
    16. central->setLayout(layout);
    17. setCentralWidget(central);
    18. }
    19. };
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QApplication app(argc, argv);
    24.  
    25. MainWindow m;
    26. m.show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. <!DOCTYPE RCC>
    2. <RCC version="1.0">
    3. <qresource>
    4. <file>res/stop.png</file>
    5. </qresource>
    6. </RCC>
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. TEMPLATE = app
    2. TARGET =
    3. RESOURCES+= simple_example.qrc
    4. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 16th December 2009, 09:45
  2. why we need Q_OBJECT???
    By phillip_Qt in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2009, 07:49
  3. When do I need Q_OBJECT?
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th February 2006, 08:15
  4. Q_object
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 7th February 2006, 17:39

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.