Results 1 to 7 of 7

Thread: Add QLineEdit in widget

  1. #1
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Add QLineEdit in widget

    Is this the proper way of adding a line edit in a widget?
    Qt Code:
    1. QLineEdit *lineEdit = new QLineEdit;
    2. layout->addWidget(linEdit);
    To copy to clipboard, switch view to plain text mode 

    I'm a total qt newbie and finding it hard to understand the documentation

    I'm using sublime text as my text editor and compiling and running the program using the terminal

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Add QLineEdit in widget

    Yes it looks ok, but we can't know for sure only from this code snippet (the layout pointer needs to have a parent)

    You can read more here: layout management documentation and then ask us specific questions when you don't understand something.

    LE: you can use Qt Creator IDE, it makes thinks more enjoyable

  3. #3
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QLineEdit in widget

    Here's my full code for some reason I can't get the line edit to display

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

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <opencv2/core/core.hpp>
    3. #include <opencv2/highgui/highgui.hpp>
    4. #include <opencv/cv.h>
    5. #include <iostream>
    6.  
    7.  
    8. MainWindow::MainWindow()
    9. {
    10. window = new QWidget;
    11. layout = new QHBoxLayout;
    12. lineEdit = new QLineEdit;
    13.  
    14. resize(463, 251);
    15.  
    16. hostImageBtn = new QPushButton("Browse Host Image", this);
    17. hostImageBtn->setMinimumWidth(151);
    18. hostImageBtn->setMinimumHeight(27);
    19. hostImageBtn->move(20, 50);
    20.  
    21. coverImageBtn = new QPushButton("Browse Cover Image", this);
    22. coverImageBtn->setMinimumWidth(151);
    23. coverImageBtn->setMinimumHeight(27);
    24. coverImageBtn->move(20, 90);
    25.  
    26. applyWatermark = new QPushButton("Apply Watermark", this);
    27. applyWatermark->setMinimumWidth(151);
    28. applyWatermark->setMinimumHeight(27);
    29. applyWatermark->move(160, 140);
    30.  
    31. layout->addWidget(hostImageBtn);
    32. layout->addWidget(coverImageBtn);
    33. layout->addWidget(applyWatermark);
    34.  
    35. layout->addWidget(lineEdit);
    36.  
    37. }
    38.  
    39. void MainWindow::createMenus()
    40. {
    41. menuBar = new QMenuBar;
    42. helpMenu = new QMenu(tr("&Help"), this);
    43. menuBar->addMenu(helpMenu);
    44. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QObject>
    6. #include <QLabel>
    7. #include <QLineEdit>
    8. #include <QFileDialog>
    9. #include <QMenuBar>
    10. #include <QPushButton>
    11. #include <QHBoxLayout>
    12.  
    13. #include <opencv2/core/core.hpp>
    14. #include <opencv2/highgui/highgui.hpp>
    15. #include <opencv/cv.h>
    16.  
    17. class MainWindow : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. MainWindow();
    23.  
    24. private slots:
    25. void createMenus();
    26. private:
    27. QMenu *helpMenu;
    28. QMenu *fileMenu;
    29. QMenuBar *menuBar;
    30.  
    31. QAction *openAct;
    32. QAction *quitAct;
    33.  
    34. QPushButton *hostImageBtn;
    35. QPushButton *coverImageBtn;
    36. QPushButton *applyWatermark;
    37.  
    38. QWidget *window;
    39.  
    40. QHBoxLayout *layout;
    41.  
    42. QLineEdit *lineEdit;
    43. };
    44.  
    45. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Any comments???

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Add QLineEdit in widget

    Simply creating a widget without a parent does not make it visible. You must add the layout to some widget (for which it will manage the arrangement of that widget's children).

    However, it won't do you much good to add it to the MainWindow, because it expects some other child widget to be set as its "central widget".

    So, you're doing at least two things wrong:

    - adding children directly to a QMainWindow widget (as you do with your three buttons)
    - not creating a central widget for your MainWindow to manage.

    The solution is to create a vanilla QWidget, add a layout (or layout hierarchy) to it, add your other widgets as children of the QWidget and set them in the layout, then set the QWidget as the central widget of your MainWindow. Your MainWindow constructor should then look like this:

    Qt Code:
    1. MainWindow::MainWindow( QWidget * parent )
    2. : QMainWindow( parent ) // you need to do this even if "parent" is NULL
    3. {
    4. window = new QWidget;
    5.  
    6. layout = new QHBoxLayout( window );
    7. lineEdit = new QLineEdit ( window );
    8.  
    9. resize(463, 251);
    10.  
    11. hostImageBtn = new QPushButton("Browse Host Image", window );
    12. hostImageBtn->setMinimumWidth(151);
    13. hostImageBtn->setMinimumHeight(27);
    14.  
    15. coverImageBtn = new QPushButton("Browse Cover Image", window );
    16. coverImageBtn->setMinimumWidth(151);
    17. coverImageBtn->setMinimumHeight(27);
    18.  
    19. applyWatermark = new QPushButton("Apply Watermark", window );
    20. applyWatermark->setMinimumWidth(151);
    21. applyWatermark->setMinimumHeight(27);
    22.  
    23. layout->addWidget(hostImageBtn);
    24. layout->addWidget(coverImageBtn);
    25. layout->addWidget(applyWatermark);
    26.  
    27. layout->addWidget(lineEdit);
    28.  
    29. createMenus();
    30.  
    31. set CentralWidget( window );
    32. }
    To copy to clipboard, switch view to plain text mode 

    This should give you a main window with 4 widgets side-by-side across the width: the three buttons followed by the line edit. Because you've resized the window and set minimum sizes for the buttons yourself, the buttons will likely very wide (151 pixels) and very tall (251 pixels), and since there are only 10 pixels left over in the width, the line edit might be clipped off unless the main window enlarges further to accommodate it. Don't know - haven't compiled or run this new code.

    A better approach would be to use Qt Designer to create and layout the widget which will serve as your central widget as a custom class derived from QWidget, and use a combination of layouts with horizontal and vertical spacers to get your window looking like you want it, and then let Qt build it for you at run time instead of manually creating and inserting all the child widgets using hand-written code. Then your constructor basically boils down to 2 lines of code:

    Qt Code:
    1. MainWindow::MainWindow( QWidget * parent )
    2. : QMainWindow( parent )
    3. {
    4. setupUi( this );
    5. createMenus();
    6. }
    To copy to clipboard, switch view to plain text mode 

    And all of your QWidget member variables go away (since you will have defined them already using Qt Designer).

  5. #5
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QLineEdit in widget

    Thanks I got it to worked!!!

    The reason why I'm hand coding it so that I'd be familiar with the code since I'm a total qt newbie but I alreadt tried qt designer and it's really easy. Unfortunately qt designer doesn't include all the code when I drag a particular feature (buttons, line edits, etc). Like for example when I drag a button into a widget It does not include QPushButton *hostImageBtn; in the .h file

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Add QLineEdit in widget

    Like for example when I drag a button into a widget It does not include QPushButton *hostImageBtn; in the .h file
    No, it isn't in your widget class .h file (e.g. MyWidget.h), but it *is* in the ui_MyWidget.h file created by the moc processor from the MyWidget.ui file created using Qt Designer. Depending on how you include the UI code, you get to it in different ways:

    1. by multiple inheritance:

    Qt Code:
    1. class MyWidget : public QWidget, public Ui::MyWidgetClass
    2. {
    3. // ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    you simply can reference it as "hostImageBtn" since it is defined as a public member of Ui:MyWidgetClass, and you call "setupUi( this );" in the MyWidget constructor.

    2. using a member variable:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. // ...
    4.  
    5. private:
    6. Ui::MyWidgetClass ui;
    7. }
    To copy to clipboard, switch view to plain text mode 

    then you get to it as "ui.hostImageBtn" and you must call "ui.setupUi( this )" in the constructor.

    3. using a member variable pointer:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. // ...
    4.  
    5. private:
    6. Ui::MyWidgetClass * ui;
    7. }
    To copy to clipboard, switch view to plain text mode 

    then you get to it as "ui->hostImageBtn" and you must call "ui = new Ui::MyWidgetClass();" and "ui->setupUi( this );" in the MyWidget constructor and "delete ui;" in the destructor.

  7. #7
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QLineEdit in widget

    Thanks I think i understand how qt works now (well at least a little bit).

Similar Threads

  1. Replies: 0
    Last Post: 14th February 2012, 12:07
  2. Deleting characters in QLineEdit widget
    By Santiago in forum Newbie
    Replies: 2
    Last Post: 11th November 2011, 14:18
  3. QlineEdit is not showing the Current Focused Widget,
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 12
    Last Post: 18th December 2010, 12:45
  4. get mousepress when widget is disabled (QLineEdit)
    By mortoray in forum Qt Programming
    Replies: 3
    Last Post: 10th November 2010, 04:44
  5. Custom widget with frame like QLineEdit
    By cil in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2010, 11:58

Tags for this Thread

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.