PDA

View Full Version : Add QLineEdit in widget



stbb24
2nd June 2012, 18:32
Is this the proper way of adding a line edit in a widget?


QLineEdit *lineEdit = new QLineEdit;
layout->addWidget(linEdit);


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

Zlatomir
2nd June 2012, 19:18
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 (http://qt-project.org/doc/qt-4.8/layout.html) and then ask us specific questions when you don't understand something.

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

stbb24
3rd June 2012, 00:49
Here's my full code for some reason I can't get the line edit to display

main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp



#include "mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
#include <iostream>


MainWindow::MainWindow()
{
window = new QWidget;
layout = new QHBoxLayout;
lineEdit = new QLineEdit;

resize(463, 251);

hostImageBtn = new QPushButton("Browse Host Image", this);
hostImageBtn->setMinimumWidth(151);
hostImageBtn->setMinimumHeight(27);
hostImageBtn->move(20, 50);

coverImageBtn = new QPushButton("Browse Cover Image", this);
coverImageBtn->setMinimumWidth(151);
coverImageBtn->setMinimumHeight(27);
coverImageBtn->move(20, 90);

applyWatermark = new QPushButton("Apply Watermark", this);
applyWatermark->setMinimumWidth(151);
applyWatermark->setMinimumHeight(27);
applyWatermark->move(160, 140);

layout->addWidget(hostImageBtn);
layout->addWidget(coverImageBtn);
layout->addWidget(applyWatermark);

layout->addWidget(lineEdit);

}

void MainWindow::createMenus()
{
menuBar = new QMenuBar;
helpMenu = new QMenu(tr("&Help"), this);
menuBar->addMenu(helpMenu);
}


mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QObject>
#include <QLabel>
#include <QLineEdit>
#include <QFileDialog>
#include <QMenuBar>
#include <QPushButton>
#include <QHBoxLayout>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private slots:
void createMenus();
private:
QMenu *helpMenu;
QMenu *fileMenu;
QMenuBar *menuBar;

QAction *openAct;
QAction *quitAct;

QPushButton *hostImageBtn;
QPushButton *coverImageBtn;
QPushButton *applyWatermark;

QWidget *window;

QHBoxLayout *layout;

QLineEdit *lineEdit;
};

#endif // MAINWINDOW_H


Any comments???

d_stranz
3rd June 2012, 01:19
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:



MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent ) // you need to do this even if "parent" is NULL
{
window = new QWidget;

layout = new QHBoxLayout( window );
lineEdit = new QLineEdit ( window );

resize(463, 251);

hostImageBtn = new QPushButton("Browse Host Image", window );
hostImageBtn->setMinimumWidth(151);
hostImageBtn->setMinimumHeight(27);

coverImageBtn = new QPushButton("Browse Cover Image", window );
coverImageBtn->setMinimumWidth(151);
coverImageBtn->setMinimumHeight(27);

applyWatermark = new QPushButton("Apply Watermark", window );
applyWatermark->setMinimumWidth(151);
applyWatermark->setMinimumHeight(27);

layout->addWidget(hostImageBtn);
layout->addWidget(coverImageBtn);
layout->addWidget(applyWatermark);

layout->addWidget(lineEdit);

createMenus();

set CentralWidget( window );
}


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:



MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{
setupUi( this );
createMenus();
}


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

stbb24
3rd June 2012, 02:05
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

d_stranz
3rd June 2012, 02:23
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:



class MyWidget : public QWidget, public Ui::MyWidgetClass
{
// ...
};

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:



class MyWidget : public QWidget
{
// ...

private:
Ui::MyWidgetClass ui;
}


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

3. using a member variable pointer:



class MyWidget : public QWidget
{
// ...

private:
Ui::MyWidgetClass * ui;
}


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.

stbb24
3rd June 2012, 06:26
Thanks I think i understand how qt works now (well at least a little bit). :)