Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access to the user interface
Forum Updated to NodeBB v4.3 + New Features

Access to the user interface

Scheduled Pinned Locked Moved General and Desktop
25 Posts 2 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leon2225
    wrote on last edited by
    #1

    Hey,
    I've got a problem. I use Qt since now a week and there's a problem wich I can't solve.
    I have added a new class, Data, wich contains all data of me projekt. The Problem is that I want to write to the mainwindow, but I've a fail from the debugger ... :
    Name of signal: SIGSEGV
    Meaning: Segmentation fault

    Here's the code, the fail apears inprintItsContent in data.cpp

    Data.cpp:
    @#include "data.h"

    Data::Data(Fxhnn * pFxhnn) : MyFxhnn(pFxhnn),itsEncryptLevel(0), itsShuffleLevel(0)
    {
    itsKey = rand() % LONG_MAX;
    }

    ...

    void Data::printItsContent(){ // <---- here's the bug
    MyFxhnn -> ui -> itsText->clear();
    return;
    }

    ...
    ...

    //Daten Speichern und öffnen
    

    bool Data::saveKeyAndMeta(){
    QString fileName = QFileDialog::getSaveFileName(0, tr("Schlüssel Speichern"), QString(), tr("Fxhnn Key Files (*.FXHNNKEY)"));
    if(!fileName.isEmpty()){
    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly)){
    QMessageBox::critical(0, tr("Error"), tr("Could not save key"));
    return false;
    }
    QDataStream stream(&file);
    stream << itsKey << itsEncryptLevel << itsShuffleLevel;
    file.close();
    }
    return true;
    }

    bool Data::saveContent(){
    QString fileName = QFileDialog::getSaveFileName(0, tr("Text Speichern"), QString(), tr("Fxhnn Content Files (*.FXHNNCON)"));

    if(!fileName.isEmpty()){
        QFile file&#40;fileName&#41;;
        if(!file.open(QIODevice::WriteOnly)){
            QMessageBox::critical(0, tr("Error"), tr("Could not save file"));
            return false;
        }
        QTextStream stream(&file);
        stream << itsContent;
        stream.flush();
        file.close();
    }
    return true;
    

    }

    bool Data::saveAll(){
    bool content = saveContent();
    QMessageBox::critical(0, tr("No Error"), tr("Could save file"));
    bool keyAndMeta = saveKeyAndMeta();

    if(content && keyAndMeta)
        return true;
    
    return false;
    

    }

    bool Data::openKeyAndMeta(){
    QString fileName = QFileDialog::getSaveFileName(0, tr("Schlüssel Öffnen"), QString(),
    tr("Fxhnn Key Files (*.FXHNNKEY)"));
    if(!fileName.isEmpty()){
    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly)){
    QMessageBox::critical(0, tr("Error"), tr("Could not open file"));
    return false;
    }
    QDataStream in(&file);
    in >> itsKey >> itsEncryptLevel >> itsShuffleLevel;
    file.close();
    }
    return true;
    }

    bool Data::openContent(){
    QString fileName = QFileDialog::getOpenFileName(0, tr("Inhalt öffnen"), QString(),
    tr("Fxhnn Content Files (*.FXHNNCON)"));

    if(!fileName.isEmpty()){
        QFile file&#40;fileName&#41;;
        if(!file.open(QIODevice::ReadOnly)){
            QMessageBox::critical(0, tr("Error"), tr("Could not open file"));
            return false;
        }
        QTextStream in(&file);
        setItsContent(in.readAll());
        printItsContent();
        file.close();
    
        if(itsContent != MyFxhnn -> ui -> itsText -> toPlainText()){
            QMessageBox::critical(0, tr("Error"), tr("Fehler beim Öffnen, Data != Textfeld"));
            return false;
        }
    
    
    }
    return true;
    

    }

    bool Data::openAll(){
    bool content = openContent();
    bool keyAndMeta = openKeyAndMeta();

    if(content && keyAndMeta)
        return true;
    
    return false;
    

    }
    @

    data.h:
    @#ifndef DATA_H
    #define DATA_H

    #include <QWidget>
    #include "fxhnn.h"
    #include "ui_fxhnn.h"
    #include <cstdlib>
    #include <limits.h>
    #include <QString>
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>
    #include <QDataStream>

    class Fxhnn;

    class Data : public QWidget
    {
    Q_OBJECT

    Fxhnn *MyFxhnn;
    

    public:
    explicit Data(Fxhnn*);
    ~Data();

        //Zugriffe auf den Inhalt
    QString getItsContent();
    void printItsContent();
    void setItsContent( QString newContent);
    void readItsContent();
    
        //Zugriffe auf den Schlüssel
    qint64 getItsKey();
    void setItsKey( qint64 newKey);
    void generateItsKey();
    
        //Zugriffe auf das Level der Verschlüsslung
    void setItsEncryptLevel( short upDownGrade = 1);
    ushort getItsEncryptLevel();
    void setItsShuffleLevel( short upDownGrade = 1);
    ushort getItsShuffleLevel();
    
        //Speichern der Daten
    bool saveKeyAndMeta();
    bool openKeyAndMeta();
    bool saveContent();
    bool openContent();
    bool saveAll();
    bool openAll();
    

    signals:

    public slots:
    ...

    private:
    qint64 itsKey;
    QString itsContent;
    ushort itsShuffleLevel;
    ushort itsEncryptLevel;
    };

    #endif // DATA_H
    @

    fxhnn.cpp:
    @
    #include "fxhnn.h"
    #include "ui_fxhnn.h"
    #include "encrypttext.h"
    #include "decrypttext.h"

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

    Data * MyData = new Data(this);
    encryptText * encryptWidget = new encryptText(this);
    decryptText * decryptWidget = new decryptText(this);
    
    ...
    

    }

    ...
    @

    fxhnn.h:
    @#ifndef FXHNN_H
    #define FXHNN_H

    #include <QMainWindow>
    #include <cstdlib>
    #include <limits.h>
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>
    #include <QDataStream>
    #include "data.h"

    namespace Ui {
    class Fxhnn;
    }

    class Data;

    class Fxhnn : public QMainWindow
    {
    Q_OBJECT

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

    Ui::Fxhnn *ui;
    
    Data *MyData;
    

    private slots:
    void changeMode();

    void on_actionLoadContent_triggered();
    
    void on_actionLoadKey_triggered();
    
    void on_actionSaveContent_triggered();
    
    void on_actionSaveKey_triggered();
    
    void on_itsText_textChanged();
    
    void on_actionAlles_Speichern_triggered();
    
    void on_actionAlles_Laden_triggered();
    

    private:

    };

    #endif // FXHNN_H
    @

    I hope that you could help me

    Leon

    PS: Sorry for the bit of german language ^^
    PPS: I don't know wich files u need ..

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      in Fhxnn constructor you have:

      @Data * MyData = new Data(this);@

      which shadows your member variable of the same name. Thus I suspect that you call MyData->something later on and thus the crash.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon2225
        wrote on last edited by
        #3

        Okey ... how can I solve the problem ?
        I can't delete @Data * MyData = new Data(this);@
        But also not the declaration in fhxnn.h ..

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What about just:

          @MyData = new Data(this);@

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon2225
            wrote on last edited by
            #5

            Oh yes .. I've been a bit tired ^^

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leon2225
              wrote on last edited by
              #6

              But it still wont open the files ... there's no error or something like that, but the stream soen't write it's data into the member variables

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Are you sure you are reading the data correctly ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leon2225
                  wrote on last edited by
                  #8

                  no ... I only meant that the compiler don't find a compile error, of course there have to be an error, if not it would work.
                  DO you have an idea where the error is? :D

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    The debugger is your friend ;)

                    since you wrote it crashes here

                    @MyFxhnn -> ui -> itsText->clear();@

                    Are you sure everything is valid ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      leon2225
                      wrote on last edited by
                      #10

                      No that problem is fixed.
                      Also the debugger doesn't say anything, the only problem is, that the function openContent() doesn't write the data into itsContent ..

                      @bool Data::openContent(){
                      QString fileName = QFileDialog::getOpenFileName(MyFxhnn, tr("Inhalt öffnen"), QString(),
                      tr("Fxhnn Content Files (*.FXHNNCON)"));
                      QFile file(fileName);
                      if(!file.open(QIODevice::ReadOnly)){
                      QMessageBox::critical(MyFxhnn, tr("Error"), tr("Could not open file"));
                      return false;
                      }
                      QTextStream in(&file);
                      in >> itsContent;
                      printItsContent();
                      file.close();

                          if(itsContent != MyFxhnn -> ui -> itsText -> toPlainText()){
                              QMessageBox::critical(MyFxhnn, tr("Error"), tr("Fehler beim Öffnen, Data != Textfeld"));
                              return false;
                          }
                      
                      
                      
                      return true;
                      

                      }@

                      Leon

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        From your code, I wouldn't expect openContent to write anything. You open the selected file in ReadOnly mode and you read from it after that.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          leon2225
                          wrote on last edited by
                          #12

                          yea I meant that the function has to "write" the data from the stream into the variable ... but I don't know, where there should be the bug :/

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Ok, so you mean that "itsContent" doesn't receive the content of the file ?

                            Following your code printItsContent just clears the itsText widget content so when you compare the content of itsText to itsContent's value, your test will only succeed if itsContent is empty.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              leon2225
                              wrote on last edited by
                              #14

                              I've changed that ... a few days ago :D
                              I'll post the new code .. :
                              data.h
                              @#ifndef DATA_H
                              #define DATA_H

                              #include <QWidget>
                              #include "fxhnn.h"
                              #include "ui_fxhnn.h"
                              #include <cstdlib>
                              #include <limits.h>
                              #include <QString>
                              #include <QFileDialog>
                              #include <QFile>
                              #include <QMessageBox>
                              #include <QTextStream>
                              #include <QDataStream>

                              class Fxhnn;

                              class Data : public QWidget
                              {
                              Q_OBJECT

                              Fxhnn *MyFxhnn;
                              

                              public:
                              explicit Data(Fxhnn*);
                              ~Data();

                                  //Zugriffe auf den Inhalt
                              QString getItsContent();
                              void printItsContent();
                              void setItsContent( QString newContent);
                              void readItsContent();
                              void reset();
                              
                              
                                  //Speichern der Daten
                              bool saveKeyAndMeta(bool customFileName = 0);
                              bool openKeyAndMeta(bool customFileName = 0);
                              bool saveContent(bool customFileName = 0);
                              bool openContent(bool customFileName = 0);
                              bool saveAll(bool customFileName = 0);
                              bool openAll(bool customFileName = 0);
                              

                              signals:

                              private:
                              qint64 itsKey;
                              QString itsContent;
                              ushort itsShuffleLevel;
                              ushort itsEncryptLevel;
                              QString itsKeyFilename;
                              QString itsContentFilename;
                              };

                              #endif // DATA_H
                              @

                              data.cpp:
                              @#include "data.h"

                              Data::Data(Fxhnn * pFxhnn) : MyFxhnn(pFxhnn),itsEncryptLevel(0), itsShuffleLevel(0)
                              {
                              itsKey = rand() % LONG_MAX;
                              setItsEncryptLevel(0);
                              }

                              Data::~Data()
                              {

                              }

                              //Zugriffe auf itsContent
                              

                              void Data::printItsContent(){
                              MyFxhnn -> ui -> itsText->clear();
                              MyFxhnn -> ui -> itsText -> setPlainText(itsContent);
                              setItsEncryptLevel(0);
                              setItsShuffleLevel(0);
                              return;
                              }

                              void Data::setItsContent(QString newContent){
                              itsContent = newContent;
                              return;
                              }

                              void Data::readItsContent(){
                              itsContent = MyFxhnn -> ui -> itsText -> toPlainText();
                              return;
                              }

                              //Zugriffe auf itsKey
                              

                              qint64 Data::getItsKey(){
                              return itsKey;
                              }

                              void Data::setItsKey(qint64 newKey){
                              itsKey = newKey;
                              return;
                              }

                              void Data::generateItsKey(){
                              itsKey = rand() % USHRT_MAX;
                              return;
                              }

                              //Zugriffe auf das Level der Verschlüsslung
                              void Data::setItsEncryptLevel( short upDownGrade){
                              itsEncryptLevel = itsEncryptLevel + upDownGrade;

                              if(itsEncryptLevel <= 0){
                                  MyFxhnn -> ui -> itsIsEncrypted -> setText("Decrypted");
                                  MyFxhnn -> ui -> itsIsEncrypted -> setStyleSheet("QLabel {color : #CC0304; }");
                              }
                              else{
                                  if(itsEncryptLevel > 0){
                                      QString Out = "Encrypt Level ";
                                      Out.append(QString("%1").arg(itsEncryptLevel));
                                      MyFxhnn -> ui-> itsIsEncrypted-> setText(Out);
                                  }
                                  else
                                      MyFxhnn -> ui -> itsIsEncrypted -> setText("Encrypted");
                                  MyFxhnn -> ui -> itsIsEncrypted -> setStyleSheet("QLabel {color : #4ECC39; }");
                              }
                              

                              }

                              ushort Data::getItsEncryptLevel(){
                              return itsEncryptLevel;
                              }

                              //Daten Speichern und öffnen
                              

                              bool Data::saveKeyAndMeta(bool customFileName){
                              if(itsKeyFilename.isEmpty() || customFileName)
                              itsKeyFilename = QFileDialog::getSaveFileName(MyFxhnn, tr("Schlüssel Speichern"), QString(), tr("Fxhnn Key Files (*.FXHNNKEY)"));
                              if(!itsKeyFilename.isEmpty()){
                              QFile file(itsKeyFilename);
                              if(!file.open(QIODevice::WriteOnly)){
                              QMessageBox::critical(0, tr("Error"), tr("Could not save key"));
                              return false;
                              }
                              QDataStream stream(&file);
                              stream << itsKey << itsEncryptLevel << itsShuffleLevel;
                              file.close();
                              }
                              return true;
                              }

                              bool Data::saveContent(bool customFileName){
                              if(itsContentFilename.isEmpty() || customFileName)
                              itsContentFilename = QFileDialog::getSaveFileName(0, tr("Text Speichern"), QString(), tr("Fxhnn Content Files (*.txt)"));

                              if(!itsContentFilename.isEmpty()){
                                  QFile file&#40;itsContentFilename&#41;;
                                  if(!file.open(QIODevice::WriteOnly)){
                                      QMessageBox::critical(0, tr("Error"), tr("Could not save file"));
                                      return false;
                                  }
                                  QTextStream stream(&file);
                                  stream << itsContent;
                                  file.close();
                              }
                              return true;
                              

                              }

                              bool Data::saveAll(bool customFileName){
                              bool content = saveContent(customFileName);
                              bool keyAndMeta = saveKeyAndMeta(customFileName);

                              if(content && keyAndMeta)
                                  return true;
                              
                              return false;
                              

                              }

                              bool Data::openKeyAndMeta( bool customFileName){
                              if(itsKeyFilename.isEmpty() || customFileName )
                              itsKeyFilename = QFileDialog::getOpenFileName(0, tr("Schlüssel Öffnen"), QString(), tr("Fxhnn Key Files (*.FXHNNKEY)"));
                              if(!itsKeyFilename.isEmpty()){
                              QFile file(itsKeyFilename);
                              if(!file.open(QIODevice::ReadOnly)){
                              QMessageBox::critical(0, tr("Error"), tr("Could not open file"));
                              return false;
                              }
                              QDataStream in(&file);
                              in >> itsKey >> itsEncryptLevel >> itsShuffleLevel;
                              file.close();
                              printItsContent();
                              }
                              return true;
                              }

                              bool Data::openContent(bool customFileName){
                              if(itsContentFilename.isEmpty() || customFileName)
                              itsContentFilename= QFileDialog::getOpenFileName(MyFxhnn, tr("Inhalt öffnen"), QString(), tr("Fxhnn Content Files (*.txt)"));

                              if(!itsContentFilename.isEmpty()){
                                  QFile file&#40;itsContentFilename&#41;;
                                  if(!file.open(QIODevice::ReadOnly)){
                                      QMessageBox::critical(MyFxhnn, tr("Error"), tr("Could not open file"));
                                      return false;
                                  }
                                  QTextStream in(&file);
                                  setItsContent(in.readAll());
                                  printItsContent();
                                  file.close();
                              
                                  if((itsContent != MyFxhnn -> ui -> itsText -> toPlainText()) || itsContent == ""){
                                      QMessageBox::critical(MyFxhnn, tr("Error"), tr("Fehler beim Öffnen, Data != Textfeld"));
                                      return false;
                                  }
                              }
                              
                              
                              return true;
                              

                              }

                              bool Data::openAll(bool customFileName){
                              bool content = openContent(customFileName);
                              bool keyAndMeta = openKeyAndMeta(customFileName);

                              if(content && keyAndMeta)
                                  return true;
                              
                              return false;
                              

                              }
                              @

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                So is it working as you expect it to ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  leon2225
                                  wrote on last edited by
                                  #16

                                  No it isn't ... it still have the problem that it couldn't read the data from the files.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Rather than
                                    @
                                    QTextStream in(&file);
                                    setItsContent(in.readAll());@

                                    Why not just:
                                    @
                                    setItsContent(file.readAll());
                                    @

                                    In any case, are you sure that your file is not empty ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      leon2225
                                      wrote on last edited by
                                      #18

                                      That doesn't work... And the file isn't empty

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        Did you check what file.readAll() contains ?

                                        since setItsContent only assign the parameter value to your itsContent member variable why not just do:

                                        @itsContent = file.readAll()@

                                        ?

                                        By the way, you should use const QString& in your setItsContent function, that will avoid unnecessary copies.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • L Offline
                                          L Offline
                                          leon2225
                                          wrote on last edited by
                                          #20

                                          Okey ehm file.readAll() return the right content of the file, the problem is, that it doesn't convert it into QString. readAll() gives back a QByteArray wich normaly could be converted in QString with for example:
                                          @itsContent = QString(file.readAll());@

                                          But it doesn't ...

                                          here is the function again:
                                          @bool Data::openContent(bool customFileName){
                                          if(itsContentFilename.isEmpty() || customFileName)
                                          itsContentFilename= QFileDialog::getOpenFileName(MyFxhnn, tr("Inhalt öffnen"), QString(), tr("Fxhnn Content Files (*.txt)"));

                                          if(!itsContentFilename.isEmpty()){
                                              QFile file&#40;itsContentFilename&#41;;
                                              if(!file.open(QIODevice::ReadOnly)){
                                                  QMessageBox::critical(MyFxhnn, tr("Error"), tr("Could not open file"));
                                                  return false;
                                              }
                                              QTextStream in(&file);
                                              QMessageBox::information(MyFxhnn, "Content of readAll(", file.readAll());
                                              itsContent = QString(file.readAll());
                                              QMessageBox::information(MyFxhnn, "Content of itsContent", QString(file.readAll()));
                                              printItsContent();
                                              file.close();
                                          
                                              if((itsContent != MyFxhnn -> ui -> itsText -> toPlainText()) || itsContent == ""){
                                                  QMessageBox::critical(MyFxhnn, tr("Error"), tr("Fehler beim Öffnen, Data != Textfeld"));
                                                  return false;
                                              }
                                          }
                                          
                                          
                                          return true;
                                          

                                          }@

                                          The Message "Content of readAll()" gives back the right content the other message return nothing an the last error message appears.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved