Skip to content

Commit

Permalink
Dialog lab solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ynonp committed Jul 21, 2011
1 parent a1af58a commit 743c361
Show file tree
Hide file tree
Showing 17 changed files with 582 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions Lab_Dialogs/Lab_Dialogs.pro
@@ -0,0 +1,21 @@
#-------------------------------------------------
#
# Project created by QtCreator 2011-07-21T09:10:01
#
#-------------------------------------------------

QT += core gui

TARGET = Lab_Dialogs
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp \
dialog.cpp

HEADERS += mainwindow.h \
dialog.h

FORMS += mainwindow.ui \
dialog.ui
46 changes: 46 additions & 0 deletions Lab_Dialogs/dialog.cpp
@@ -0,0 +1,46 @@
#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>

Dialog::Dialog(int val, QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);

QObject::connect(ui->buttonBox, SIGNAL(rejected()),
this, SLOT(reject()));

QObject::connect(ui->buttonBox, SIGNAL(accepted()),
this, SLOT(validate()));

ui->spinBox->setValue(val);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::validate()
{
int val = value();

if ( val < 50 )
{
accept();
return;
}
else
{
QMessageBox::warning(this, tr("Value too high"),
tr("Please type a value < 50"));

}
}

int Dialog::value()
{
return ui->spinBox->value();
}

26 changes: 26 additions & 0 deletions Lab_Dialogs/dialog.h
@@ -0,0 +1,26 @@
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(int val, QWidget *parent = 0);
~Dialog();

int value();
private slots:
void validate();

private:
Ui::Dialog *ui;
};

#endif // DIALOG_H
71 changes: 71 additions & 0 deletions Lab_Dialogs/dialog.ui
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QSpinBox" name="spinBox"/>
</item>
<item>
<widget class="QSlider" name="horizontalSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>horizontalSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>spinBox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>53</x>
<y>146</y>
</hint>
<hint type="destinationlabel">
<x>83</x>
<y>70</y>
</hint>
</hints>
</connection>
<connection>
<sender>spinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>horizontalSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>215</x>
<y>66</y>
</hint>
<hint type="destinationlabel">
<x>226</x>
<y>144</y>
</hint>
</hints>
</connection>
</connections>
</ui>
11 changes: 11 additions & 0 deletions Lab_Dialogs/main.cpp
@@ -0,0 +1,11 @@
#include <QtGui/QApplication>
#include "mainwindow.h"

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

return a.exec();
}
64 changes: 64 additions & 0 deletions Lab_Dialogs/mainwindow.cpp
@@ -0,0 +1,64 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
Dialog d(ui->horizontalSlider->value());
if ( d.exec() == QDialog::Accepted )
{
ui->horizontalSlider->setValue(d.value());
}
}

void MainWindow::on_pushButton_2_clicked()
{
Dialog *d = new Dialog(ui->horizontalSlider->value(), this);
d->show();
d->raise();
d->activateWindow();

QObject::connect(d, SIGNAL(accepted()),
this, SLOT(dialogAccepted()));
QObject::connect(d, SIGNAL(rejected()),
this, SLOT(dialogRejected()));
}

void MainWindow::dialogAccepted()
{
Dialog *d = qobject_cast<Dialog *>(sender());

if ( ! d )
{
return;
}

int val = d->value();
ui->horizontalSlider->setValue(val);
d->deleteLater();
}

void MainWindow::dialogRejected()
{
Dialog *d = qobject_cast<Dialog *>(sender());
if ( ! d )
{
return;
}

d->deleteLater();
}

30 changes: 30 additions & 0 deletions Lab_Dialogs/mainwindow.h
@@ -0,0 +1,30 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void dialogAccepted();
void dialogRejected();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
68 changes: 68 additions & 0 deletions Lab_Dialogs/mainwindow.ui
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QSlider" name="horizontalSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Modal</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>Modeless</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
2 changes: 1 addition & 1 deletion QoolEdit/resources.qrc
@@ -1,6 +1,6 @@
<RCC>
<qresource prefix="/">
<file>img/LogoZone-60970253142601.JPG</file>
<file>img/Close Icon.jpg</file>
<file alias="foo">img/Close Icon.jpg</file>
</qresource>
</RCC>
11 changes: 11 additions & 0 deletions SignalMapper/SignalMapper.pro
@@ -0,0 +1,11 @@
SOURCES += \
main.cpp \
dialog.cpp \
dialoglauncher.cpp

HEADERS += \
dialog.h \
dialoglauncher.h

FORMS += \
dialog.ui
20 changes: 20 additions & 0 deletions SignalMapper/dialog.cpp
@@ -0,0 +1,20 @@
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);

QObject::connect(ui->buttonBox, SIGNAL(accepted()),
this, SLOT(accept()));

QObject::connect(ui->buttonBox, SIGNAL(rejected()),
this, SLOT(reject()));
}

Dialog::~Dialog()
{
delete ui;
}

0 comments on commit 743c361

Please sign in to comment.