Pop up examples
-
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();
}
@ -
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();
}
@ -
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();
}@ -
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. -
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.
-
(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