IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Discussion :

Calcul de pourcentages dans plusieurs QTextEdit

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2007
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 52
    Points : 34
    Points
    34
    Par défaut Calcul de pourcentages dans plusieurs QTextEdit
    Bonjour,
    je vous contact car je me heurte a un souci dans la programmation de ma fonction/logiciel.

    Le projet : reussir a faire un genre de calculette avec 4 cases pour calculer un pourcentage.

    Une image vaut mieux que mille mots :



    Ce que je souhaite c'est que l'utilisateur puisse saisir les informations dans les cases 1, 2 et 4 (Valeur intermediaire, total, 100) et qu'en cliquant sur Calculer le resultat apparaisse.

    Exemple : J'ai lu 400 pages sur un livre qui en compte 800 je fais donc (400x100)/800 ce qui devrait afficher 50 ^^

    Voici les differents partie de code que jai pu faire :

    pourcent-calc.pro
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #-------------------------------------------------
    #
    # Project created by QtCreator 2017-10-15T02:21:34
    #
    #-------------------------------------------------
     
    QT       += core gui
     
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     
    TARGET = pourcent-calc
    TEMPLATE = app
     
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
     
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
     
     
    SOURCES += \
            main.cpp \
            mainwindow.cpp
     
    HEADERS += \
            mainwindow.h
     
    FORMS += \
            mainwindow.ui
    mainwindow.h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
     
    #include <QMainWindow>
     
    using namespace std;
     
    namespace Ui {
    class MainWindow;
    }
     
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
     
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
     
    private slots:
     
        void on_pushButton_clicked();
     
    private:
        Ui::MainWindow *ui;
    };
     
    #endif // MAINWINDOW_H
    main.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include "mainwindow.h"
    #include <QApplication>
     
    using namespace std;
     
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
     
        return a.exec();
    }
    mainwindow.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QApplication>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        valeurInter = (textEdit)findViewById(R.id.textEditvaleurInter);
        valeurInter = valeurInter.getText().toString();
    }
    J'ai un gros doute sur la dernière partie en gras...

    Pouvez vous m'aider ou me donner un élément de réponse svp ?

    Merci et a très vite
    mcvovol

  2. #2
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2010
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2010
    Messages : 248
    Points : 421
    Points
    421
    Par défaut
    Bonjour,

    Le code en gras est du code Android !? findViewById() n'existe pas dans l'environnement Qt, de même que la variable "R".

    Le code serait plutôt:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    bool ok;
    int vInter = ui->textEditvaluerInter.text().toInt(&ok);
    if(ok){
        //vInter contient la valeur intermédiaire
        int vRes = (vInter / vTotal) * 100;
        //Affichage du résultat
        ui->textEditResultat.setText(QString::number(vRes));
    } else {
        //Erreur de format
    }

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2007
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 52
    Points : 34
    Points
    34
    Par défaut
    Bonjour et merci pour votre réponse très précise.

    Je me suis permis d'adapter le code a mon programme mais il y a un souci.

    Voici le code que j'ai en cliquant sur " Calculer " :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    void MainWindow::on_pushButton_clicked()
    {
     
        bool ok;
        int vTotal = 0;
        int vInter = ui->vInter.text().toInt(&ok);
        if(ok){
            //vInter contient la valeur intermédiaire
            int vRes=(vInter/vTotal)*100;
            //Affichage du résultat
            ui->vRes.setText(QString::number(vRes));
        } else {
            //Erreur de format
        }
    }
    Il y a une erreur a ce niveau :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int vInter = ui->vInter.text().toInt(&ok);
    C:\Users\mcvov\Documents\Qt\Pourcent-calc\pourcent-calc\mainwindow.cpp:22: erreur : request for member 'text' in '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::vInter', which is of pointer type 'QTextEdit*' (maybe you meant to use '->' ?)
    int vInter = ui->vInter.text().toInt(&ok);
    ---------------------^
    et

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ui->vRes.setText(QString::number(vRes));
    C:\Users\mcvov\Documents\Qt\Pourcent-calc\pourcent-calc\mainwindow.cpp:27: erreur : request for member 'setText' in '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::vRes', which is of pointer type 'QTextEdit*' (maybe you meant to use '->' ?)
    ui->vRes.setText(QString::number(vRes));
    ---------^

    Je ne comprends pas bien comment corriger ces erreurs la.

    Merci de votre aide et bonne soirée

    mcvovol

    P.S. : j'ai mis du texte blanc avant les " ^ " histoire qu'ils tombent au bon endroit sur la reponse

  4. #4
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2010
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2010
    Messages : 248
    Points : 421
    Points
    421
    Par défaut
    L'objet "ui->vRes" étant un pointeur, il faut utiliser l'opérateur "->" et non "."

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    int vInter = ui->vInter->text().toInt(&ok);
    ui->vRes->setText(QString::number(vInter));

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2007
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 52
    Points : 34
    Points
    34
    Par défaut
    Bonjour,

    La deuxième ligne fonctionne parfaitement alors que la première bug toujours

    voici le message d'erreur pour :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int vInter = ui->vInter->text().toInt(&ok);
    C:\Users\mcvov\Documents\Qt\Pourcent-calc\pourcent-calc\mainwindow.cpp:22: erreur : 'class QTextEdit' has no member named 'text'
    int vInter = ui->vInter->text().toInt(&ok);
    -----------------------^

    Le " ^ " pointe sous text()

    Merci de votre aide,
    mcvovol

  6. #6
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2010
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2010
    Messages : 248
    Points : 421
    Points
    421
    Par défaut
    Pardon, c'est l'habitude de QLineEdit. Avec QTextEdit, il faut utiliser 'toPlaintext()', ou 'toHtml()'.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int vInter = ui->vInter->toPlaintext().toInt(&ok);

Discussions similaires

  1. Réponses: 9
    Dernier message: 17/05/2010, 14h26
  2. [AC-2007] Calcul de pourcentage dans une requête
    Par Nephyline dans le forum Requêtes et SQL.
    Réponses: 8
    Dernier message: 05/10/2009, 15h31
  3. Réponses: 9
    Dernier message: 21/02/2008, 10h12
  4. calcul de pourcentage dans requête
    Par Eruil dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 17/07/2007, 15h29
  5. Réponses: 7
    Dernier message: 04/06/2007, 13h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo