Dedicado a mis proyectos en Gambas ,un lenguaje de programación parecido al Visual Basic + Java pero ampliamente mejorado y...¡¡para gnu/linux!!.La potencia del gnu/linux sumada a la facilidad del Basic



Consultas, Desarrollo de programas y petición de presupuestos:



domingo, 5 de febrero de 2012

QT4: ejemplo de cuadros de dialogos...

He estado viendo la documentación de QT4, esta muy completa e interesante.
He hecho un pequeño ejemplo de como se pueden programar los cuadros de diálogos, que  es más corto del que viene en la documentación..

Se hace creando 3 archivos (con el gedit, por ejemplo):
1) El archivo main.cpp:

#include <QApplication>
#include "dialogo.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
dialogo w; //instanciamos una clase dialogo
w.show(); //mostramos en pantalla
return a.exec();
}

2) El archivo dialogo.h

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QErrorMessage;
class dialogo : public QDialog
{
Q_OBJECT
public:
dialogo(QWidget *parent = 0);
 
private slots:
void setInteger();
void setDouble();
void setItem();
void setText();

private:
QCheckBox *native;
QLabel *integerLabel;
QLabel *doubleLabel;
QLabel *itemLabel;
QLabel *textLabel;
};
#endif


3) El archivo dialogo.cpp:

#include <QColorDialog>
#include <QGridLayout>
#include <QInputDialog>
#include <QLabel>
#include <QPushButton>
#include "dialogo.h"
#define MESSAGE \
Dialogo::tr("<p>Message boxes have a caption, a text, " \
"and any number of buttons, each with standard or custom texts." \
"<p>Click a button to close the message box. Pressing the Esc button " \
"will activate the detected escape button (if any).")
dialogo::dialogo(QWidget *parent)
: QDialog(parent)
{
//definimos los botones y label que tendra el formulario inicial 
int frameStyle = QFrame::Sunken | QFrame::Panel;
integerLabel = new QLabel;
integerLabel->setFrameStyle(frameStyle);
QPushButton *integerButton =
new QPushButton(tr("QInputDialog::get&Int()"));
doubleLabel = new QLabel;
doubleLabel->setFrameStyle(frameStyle);
QPushButton *doubleButton =
new QPushButton(tr("QInputDialog::get&Double()"));
itemLabel = new QLabel;
itemLabel->setFrameStyle(frameStyle);
QPushButton *itemButton = new QPushButton(tr("QInputDialog::getIte&m()"));
textLabel = new QLabel;
textLabel->setFrameStyle(frameStyle);
QPushButton *textButton = new QPushButton(tr("QInputDialog::get&Text()"));

 
// concectamos las señales con los slot: (eventos)
connect(integerButton, SIGNAL(clicked()), this, SLOT(setInteger()));
connect(doubleButton, SIGNAL(clicked()), this, SLOT(setDouble()));
connect(itemButton, SIGNAL(clicked()), this, SLOT(setItem()));
connect(textButton, SIGNAL(clicked()), this, SLOT(setText()));


//definicion de la ventana, y colocacion de los botones, y textos
QGridLayout *layout = new QGridLayout;
layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(1, 250);
layout->addWidget(integerButton, 0, 0);
layout->addWidget(integerLabel, 0, 1);
layout->addWidget(doubleButton, 1, 0);
layout->addWidget(doubleLabel, 1, 1);
layout->addWidget(itemButton, 2, 0);
layout->addWidget(itemLabel, 2, 1);
layout->addWidget(textButton, 3, 0);
layout->addWidget(textLabel, 3, 1);

setLayout(layout);
setWindowTitle(tr("Algunos Dialogos Standard de QT4"));
}
// definimos lo que hacen cada método
void dialogo::setInteger()
{
bool ok;
//QinputDialog::getInt, devuelve un INT  (integer)  , y en la funcion le damos los valores iniciales, boton que aparezca, etc.. (ver documentación)
int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
tr("Percentage:"), 25, 0, 100, 1, &ok);
if (ok)
//asignamos el valor elegido al IntegerLabel   
integerLabel->setText(tr("%1%").arg(i));
}
 

void dialogo::setDouble()
{
bool ok;
double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
if (ok)
//asignamos el valor elegido al doubleLabel 
doubleLabel->setText(QString("$%1").arg(d));
}
 
// un combo: el usuario eligira entre varios valores (dado por items),  un valor
void dialogo::setItem()
{
QStringList items;
items << tr("Primavera") << tr("Verano") << tr("Otoño") << tr("Invierno");
bool ok;
QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
tr("Estacion:"), items, 0, false, &ok);
if (ok && !item.isEmpty())
//asignamos el valor elegido al itemLabel 
itemLabel->setText(item);
}
 
// peguntar por un texto...
void dialogo::setText()
{
bool ok;
QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
tr("Escribe un texto:"), QLineEdit::Normal,
hola”, &ok);
if (ok && !text.isEmpty())
//asignamos el valor elegido al textLabel  
textLabel->setText(text);
}

Estos 3 archivos lo metes en una carpeta, por ejemplo "/home/usario/pruebasqt"
Y para compilarlo:
$ qmake -project
$ qmake
$ make

Y creara un ejecutable llamado "pruebasqt", que los podeis ejecutar en linux poniendo:
$ .\pruebasqt

Fuente:
http://www.zonaqt.com/tutoriales/tutorial-b%C3%A1sico-de-qt-4-11

Enlace interesante: http://dibosa.wordpress.com/articulos-programacion/desarrollando-aplicaciones-opensource-multiplataforma-con-las-librerias-qt4-y-c/