#1 Le 28/03/2008, à 23:20
- spymaster
Comment bloquer/débloqué un bouton avec Qt
Bonjour, je commence tout juste a apprendre a utiliser la librairie Qt, j'ai suivi le cours que le site du zero et j'ai compris l'essensiel, seulement je me posait une question.
Peut on bloquer un bouton afin q'in soit imposible de cliquer dessus (faire comme un hide() sauf qu'on voie le bouton).
J'ai développer un petit jeu (m'inspirant du David et Goliath du site du zero ) et pour le moment je me suis contenté de cacher le bouton seulement c'est pas tres élégant.
J'ai une petite question aussi quel livre traitant de Qt 4 me conseillé vous d'acheter affin que persicte dans l'aprentisage de Qt
Merci d'avance a bientôt
Clément
Hors ligne
#2 Le 29/03/2008, à 01:07
- Link31
Re : Comment bloquer/débloqué un bouton avec Qt
button->setEnabled(false);
Hors ligne
#3 Le 29/03/2008, à 03:31
- obiwankennedy
Re : Comment bloquer/débloqué un bouton avec Qt
perso, Qt4 et C++ programmation d'interface GUI est pas mal. c'est un peu la réference. il traite d'un peu tout.
Dans mes logiciels, j'écris ton nom.
SGNGD: SvgGd is Not GD
Rolisteam
Hors ligne
#4 Le 29/03/2008, à 15:25
- spymaster
Re : Comment bloquer/débloqué un bouton avec Qt
Merci, pour ceux a qui ca interesse de modifier mon petit programme qui oppose david et goliath je vous donne les sources
main.cpp
#include <QtGui>
#include "arme.h"
#include "personnage.h"
#include "fenetre.h"
int main(int argv, char* argc[])
{
QApplication app(argv, argc);
Fenetre fenetre;
fenetre.show();
return app.exec();
}
fenetre.h
#ifndef FENETRE_H_INCLUDED
#define FENETRE_H_INCLUDED
#include <QtGui>
#include "personnage.h"
class Fenetre : public QWidget
{
Q_OBJECT
public :
Fenetre();
public slots:
void AttaqueDavid();
void AttaqueGoliath();
void DavidBoirePotion();
void GoliathBoirePotion();
private :
QPushButton *m_DavidAttaque;
QPushButton *m_GoliathAttaque;
QPushButton *m_DavidBoirePotion;
QPushButton *m_GoliathBoirePotion;
QLabel *m_LabelVieDavid;
QLabel *m_LabelVieGoliath;
QLabel *m_LabelDavid;
QLabel *m_LabelGoliath;
Personnage m_David;
Personnage m_Goliath;
};
#endif // FENETRE_H_INCLUDED
fenetre.cpp
#include "fenetre.h"
Fenetre::Fenetre() : QWidget()
{
//Taile de la fenetre fixe a 360 par 120 px
setFixedSize(360, 120);
//Afficher David :
m_LabelDavid = new QLabel("<b>David :</b>", this);
m_LabelDavid->move(10, 10);
//Afficher Goliath
m_LabelGoliath = new QLabel("<b>Goliath :</b>", this);
m_LabelGoliath->move(200, 10);
//Afficher le bouton Attaquer pour attaquer Goliath
m_DavidAttaque = new QPushButton("Attaquer", this);
m_DavidAttaque->setGeometry(10, 30, 150, 30);
m_DavidAttaque->setIcon(QIcon("epee.gif"));
//Afficher le bouton attaquer pour attaquer David
m_GoliathAttaque = new QPushButton("Attaquer", this);
m_GoliathAttaque->setGeometry(200, 30, 150, 30);
m_GoliathAttaque->setIcon(QIcon("epee.gif"));
//Bouton (david) boire de la potion
m_DavidBoirePotion = new QPushButton("Boire Potion", this);
m_DavidBoirePotion->setGeometry(10, 65, 150, 30);
m_DavidBoirePotion->setIcon(QIcon("potion.png"));
//Bouton (Goliath) boire de la potion
m_GoliathBoirePotion = new QPushButton("Boire Potion", this);
m_GoliathBoirePotion->setGeometry(200, 65, 150, 30);
m_GoliathBoirePotion->setIcon(QIcon("potion.png"));
//Afficher la vie de david
m_LabelVieDavid = new QLabel("Vie : " + QString::number(m_David.getVie()) + " / 100", this);
m_LabelVieDavid->move(10, 95);
//Afficher la vie de Goliath
m_LabelVieGoliath = new QLabel("Vie : " + QString::number(m_Goliath.getVie()) + " / 100", this);
m_LabelVieGoliath->move(200, 95);
//Signaux / Slots
QObject::connect(m_DavidAttaque, SIGNAL(clicked()), this, SLOT(AttaqueGoliath()));
QObject::connect(m_GoliathAttaque, SIGNAL(clicked()), this, SLOT(AttaqueDavid()));
QObject::connect(m_DavidBoirePotion, SIGNAL(clicked()), this, SLOT(DavidBoirePotion()));
QObject::connect(m_GoliathBoirePotion, SIGNAL(clicked()), this, SLOT(GoliathBoirePotion()));
}
void Fenetre::AttaqueDavid()
{
//Attaquer david
m_Goliath.Attaquer(m_David);
m_GoliathAttaque->setEnabled(false);
m_GoliathBoirePotion->setEnabled(false);
m_DavidAttaque->setEnabled(true);
m_DavidBoirePotion->setEnabled(true);
//si le vie de david est Inferieur ou egale a 10 alors afficher le vie en rouge
if(m_David.getVie() <= 10)
{
m_LabelVieDavid->setText("Vie : <font color='red'>" + QString::number(m_David.getVie()) + " / 100</font>");
}
//sinon l'afficher en noir
else
{
m_LabelVieDavid->setText("Vie : " + QString::number(m_David.getVie()) + " / 100");
}
}
void Fenetre::AttaqueGoliath()
{
//David attaque goliath
m_David.Attaquer(m_Goliath);
m_GoliathAttaque->setEnabled(true);
m_GoliathBoirePotion->setEnabled(true);
m_DavidAttaque->setEnabled(false);
m_DavidBoirePotion->setEnabled(false);
//Si la vie de goliath est inferieur ou egale a 10 afficher la vie en rouge
if(m_Goliath.getVie() <= 10)
{
m_LabelVieGoliath->setText("Vie : <font color='red'>" + QString::number(m_Goliath.getVie()) + "</font> / 100");
}
//Sinon afficher la vie en noir.
else
{
m_LabelVieGoliath->setText("Vie : " + QString::number(m_Goliath.getVie()) + " / 100");
}
}
void Fenetre::DavidBoirePotion()
{
//David bois de la potion (20pt de vie)
m_David.BoirePotion(20);
m_GoliathAttaque->setEnabled(true);
m_GoliathBoirePotion->setEnabled(true);
m_DavidAttaque->setEnabled(false);
m_DavidBoirePotion->setEnabled(false);
//Si la vie de david est inferieur ou egale a 10 alors afficher en rouge la vie
if(m_David.getVie() <= 10)
{
m_LabelVieDavid->setText("Vie : <font color='red'>" + QString::number(m_David.getVie()) + " / 100</font>");
}
//Sinon afficher la vie en noir
else
{
m_LabelVieDavid->setText("Vie : " + QString::number(m_David.getVie()) + " / 100");
}
}
void Fenetre::GoliathBoirePotion()
{
//Goliath bois de la potion (20 pts de vie)
m_Goliath.BoirePotion(20);
m_GoliathAttaque->setEnabled(false);
m_GoliathBoirePotion->setEnabled(false);
m_DavidAttaque->setEnabled(true);
m_DavidBoirePotion->setEnabled(true);
//Si la vie de goliath est inferieur ou egale a 10 alors afficher la vie en rouge
if(m_Goliath.getVie() <= 10)
{
m_LabelVieGoliath->setText("Vie : <font color='red'>" + QString::number(m_Goliath.getVie()) + "</font> / 100");
}
//Sinon afficher la vie en noir
else
{
m_LabelVieGoliath->setText("Vie : " + QString::number(m_Goliath.getVie()) + " / 100");
}
}
personnage.h
#ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED
#include "arme.h"
class Personnage
{
public :
Personnage();
void Attaquer(Personnage &enemi);
void RecevoirDegats(int);
void BoirePotion(int);
int getVie();
private :
int m_Vie;
int m_Mana;
Arme m_Arme;
};
#endif // PERSONNAGE_H_INCLUDED
personnage.cpp
#include "personnage.h"
Personnage::Personnage()
{
//initialiser le mana et la vie a 100
m_Vie = 100;
m_Mana = 100;
}
void Personnage::Attaquer(Personnage &enemi)
{
//Faire recevoir des dégats a l'enemi
enemi.RecevoirDegats(m_Arme.getDegats());
}
void Personnage::RecevoirDegats(int degats)
{
//suprimer de la vie
m_Vie -= degats;
//Si le vie est inferiur a 0 alors metre la vie a 0
if(m_Vie < 0)
{
m_Vie = 0;
}
}
void Personnage::BoirePotion(int vie)
{
//Ajouter de la vie
m_Vie += vie;
//Si la vie est superieur a 100 alors mettre la vie a 100;
if(m_Vie > 100)
{
m_Vie = 100;
}
}
int Personnage::getVie()
{
//Retourner la valeur de la vie du personnage
return m_Vie;
}
arme.h
#ifndef ARME_H_INCLUDED
#define ARME_H_INCLUDED
#include <string>
class Arme
{
public :
Arme();
int getDegats();
private :
std::string m_Nom;
int m_Degats;
};
#endif // ARME_H_INCLUDED
arme.cpp
#include "arme.h"
Arme::Arme()
{
//Toujours commencer avec une épée rouillé infligant 3 dégats
m_Nom = "eppee rouille";
m_Degats = 3;
}
int Arme::getDegats()
{
//Retourne les degats que fonts l'arme
return m_Degats;
}
Merci pour votre rapidité a repondre
a bientôt clément
ps : je vais essaillé de trouver le livre que tu m'a conseille
Hors ligne
#5 Le 29/03/2008, à 15:41
- spymaster
Re : Comment bloquer/débloqué un bouton avec Qt
Pour les icons j'ai pris celles ci et
Hors ligne