Pop up examples
-
wrote on 18 May 2011, 15:35 last edited by
Hello,
I am new in Qt and I have a MainWindow with a toolbar, which contains a button. When I push this button I want to pop up an other form named Window. Could someone suggest any good pop up example to beggin with?
Thanks
-
wrote on 18 May 2011, 15:56 last edited by
Hi jk_mk,
-you can just simply make your "Window'.
-include the header where you want to use it.
-call Window.show() or .exec() to get "Window" shown and useable. -
wrote on 18 May 2011, 17:46 last edited by
Thanks for your response,
I am using Qt added in visual studio 2010. So, for my pop up Window I have created this files (main.cpp window.h and window.cpp) and with the same way I have created (main.cpp MainWindow.h and MainWindow.cpp) for my main form. I add the window.h file to the header files of the MainWindow and the window.cpp to the resource files of the MainWindow. But my compiler still can not recognize the window.h file.
What I am doing wrong?Could somebody help me? -
wrote on 18 May 2011, 17:48 last edited by
It will be better to start with widget (QWidget) as window, MainWindow(QMainWindow) is different and quite harder...
-
wrote on 18 May 2011, 17:51 last edited by
Yes, you have right. I am using QWidget so far, but I went a little bit confused with the terminoly
-
wrote on 18 May 2011, 18:13 last edited by
Could somebody give me an advise?
Thanks -
wrote on 18 May 2011, 18:17 last edited by
Ah, okay, I thought, you've inherited MainWindow from QWidget not from QMainWindow...okay then...
This is KISS code, which I can write from my head...
This is the smallest code:
@
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QWidget>
#include <QObject>class MainWindow : public QWidget
{
Q_OBJECT
public slots:
void buttonPressed();
};void MainWindow::buttonPressed()
{
QMessageBox::information(0, QString("Information"), QString("You've pressed the button "Press Me!""), QMessageBox::Ok);
}int main(int argc, char* argv[])
{
QApplication a(argc,argv);MainWindow mW;
QPushButton* PressMe = new QPushButton("Press me!", &MainWindow);
QObject::connect(PressMe, SIGNAL(triggered()), mW, SLOT(buttonPressed()));
mW.show();return a.exec();
}
@ -
wrote on 18 May 2011, 18:48 last edited by
Thanks for your reply.And to be more specific I have created the following code which I want to use it as a pop up window when I press a button in an other Widget. What sould I do in Qt visual studio add-in, to compine this application?
@
////////// window.h ///////////
#ifndef WINDOW_H
#define WINDOW_H#include <QDialog>
class QComboBox;
class QDir;
class QLabel;
class QPushButton;class Window : public QDialog
{
Q_OBJECTpublic:
Window(QWidget *parent = 0);private slots:
void browse();private:
QPushButton *createButton(const QString &text, const char *member); QComboBox *createComboBox(const QString &text = QString()); QComboBox *directoryComboBox; QLabel *directoryLabel; QPushButton *browseButton;
QPushButton *openButton;
};
#endif
@@
////////// window.cpp /////////////
#include <QtGui>#include "window.h"
Window::Window(QWidget *parent)
: QDialog(parent)
{browseButton = createButton(tr("&Browse..."), SLOT(browse()));
openButton = createButton(tr("&Open"), SLOT(open()));
directoryComboBox = createComboBox(QDir::currentPath()); directoryLabel = new QLabel(tr("In directory:")); QHBoxLayout *buttonsLayout = new QHBoxLayout; buttonsLayout->addStretch();
buttonsLayout->addWidget(openButton);
QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(directoryLabel, 2, 0); mainLayout->addWidget(directoryComboBox, 2, 1); mainLayout->addWidget(browseButton, 2, 2); mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3); setLayout(mainLayout); setWindowTitle(tr("Open Files")); resize(500, 200);
}
void Window::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Open Files"), QDir::currentPath());
if (!directory.isEmpty()) {
directoryComboBox->addItem(directory);
directoryComboBox->setCurrentIndex(directoryComboBox->currentIndex() + 1);
}
}QPushButton *Window::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}QComboBox *Window::createComboBox(const QString &text)
{
QComboBox *comboBox = new QComboBox;
comboBox->setEditable(true);
comboBox->addItem(text);
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
return comboBox;
}
@@
/////////// main.cpp //////////
#include <QApplication>#include "window.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
@ -
wrote on 18 May 2011, 18:50 last edited by
sorry for the previous format:
@/////////////window.h //////////
#ifndef WINDOW_H
#define WINDOW_H#include <QDialog>
class QComboBox;
class QDir;
class QLabel;
class QPushButton;class Window : public QDialog
{
Q_OBJECTpublic:
Window(QWidget *parent = 0);private slots:
void browse();private:
QPushButton *createButton(const QString &text, const char *member); QComboBox *createComboBox(const QString &text = QString()); QComboBox *directoryComboBox; QLabel *directoryLabel; QPushButton *browseButton;
QPushButton *openButton;
};
#endif
////// window.cpp/////////
#include <QtGui>#include "window.h"
Window::Window(QWidget *parent)
: QDialog(parent)
{browseButton = createButton(tr("&Browse..."), SLOT(browse()));
openButton = createButton(tr("&Open"), SLOT(open()));
directoryComboBox = createComboBox(QDir::currentPath()); directoryLabel = new QLabel(tr("In directory:")); QHBoxLayout *buttonsLayout = new QHBoxLayout; buttonsLayout->addStretch();
buttonsLayout->addWidget(openButton);
QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(directoryLabel, 2, 0); mainLayout->addWidget(directoryComboBox, 2, 1); mainLayout->addWidget(browseButton, 2, 2); mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3); setLayout(mainLayout); setWindowTitle(tr("Open Files")); resize(500, 200);
}
void Window::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Open Files"), QDir::currentPath());
if (!directory.isEmpty()) {
directoryComboBox->addItem(directory);
directoryComboBox->setCurrentIndex(directoryComboBox->currentIndex() + 1);
}
}QPushButton *Window::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}QComboBox *Window::createComboBox(const QString &text)
{
QComboBox *comboBox = new QComboBox;
comboBox->setEditable(true);
comboBox->addItem(text);
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
return comboBox;
}/////////// main.cpp /////////
#include <QApplication>#include "window.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}@ -
wrote on 18 May 2011, 20:48 last edited by
Hi jk_mk,
next tme, please edit your last post, it's simple:
under your avatar icon, click on edit :-) -
wrote on 18 May 2011, 20:58 last edited by
what you should do? I didn't have VS add-in, so I don't know...
And now is here the code twice, good :)
-
wrote on 18 May 2011, 22:27 last edited by
I have created the this code which I want to use it as a pop up window when I press a button in an other Widget
-
wrote on 19 May 2011, 04:42 last edited by
bq. Thanks for your reply.And to be more specific I have created the following code which I want to use it as a pop up window when I press a button in an other Widget. What sould I do in Qt visual studio add-in, to compine this application?
jk_mk, your question is quite unclear to me.
Please rephrase it and tell us, what your problem is. -
wrote on 19 May 2011, 06:11 last edited by
Note that you do not need to add .cpp files to resources. Resources are for things you want to have available in your application at runtime and do not want to drop onto the filesystem. Stuff needed at compile time (like headers and sources) should never go into the resources.
-
wrote on 19 May 2011, 16:28 last edited by
(at)Gerolf: He wants to compile it with Visual Studio Add-in, but probably he doesn't know what to press/set-up...
(at)jk-mk: I advise you look at function QObject::connect() with this structure:@QObject::connect(sender, signal, reciever, slot);@
Better written: "Signals & Slots":http://doc.qt.nokia.com/latest/signalsandslots.html
1/15