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. Updating QLineEdit Text Causing Crash
Forum Updated to NodeBB v4.3 + New Features

Updating QLineEdit Text Causing Crash

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.0k 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.
  • A Offline
    A Offline
    aburritojr
    wrote on last edited by
    #1

    Hi,
    Qt beginner here...I'm using Qt Creator 2.8.1 with Qt 5.1.1 on Ubuntu 12.04.

    Part of the app I'm writing is a file uploader and I'm trying to create a slot so that when the user selects a file, the QLineEdit is updated to reflect the file name. Here's my code:

    topcash.cpp
    @#include "topcash.h"

    Topcash::Topcash(){
    createUploadArea();
    createQuoteArea();
    createFinalArea();

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(uploadLayout);
    mainLayout->addWidget(quoteLayout);
    mainLayout->addWidget(finalLayout);
    setLayout(mainLayout);
    

    }

    void Topcash::createUploadArea(){
    uploadLayout = new QGroupBox(tr("1) Select file to upload"));
    QVBoxLayout *layout = new QVBoxLayout;

    QGroupBox *upload_horiz = new QGroupBox();
    QHBoxLayout *upload_layout = new QHBoxLayout;
    upload_layout->setAlignment(Qt::AlignLeft);
    
    QLineEdit *upload_box = new QLineEdit;
    upload_box->setMinimumWidth(400);
    upload_box->setMaximumWidth(400);
    upload_box->setReadOnly(true);
    upload_box->setStyleSheet("background-color:#D8D8D8;border:1px solid #B0B0B0;border-radius:3px;");
    
    QPushButton *browse_btn = new QPushButton(tr("Browse"));
    browse_btn->setMaximumWidth(80);
    connect(browse_btn, SIGNAL(clicked()), this, SLOT(open()));
    
    upload_layout->addWidget(upload_box);
    upload_layout->addWidget(browse_btn);
    upload_horiz->setLayout(upload_layout);
    
    QPushButton *upload_button = new QPushButton(tr("Upload"));
    upload_button->setMaximumWidth(80);
    
    layout->addWidget(upload_horiz);
    layout->addWidget(upload_button);
    uploadLayout->setLayout(layout);
    

    }

    void Topcash::createQuoteArea(){
    quoteLayout = new QGroupBox(tr("2) Quote List:"));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setAlignment(Qt::AlignTop);
    
    QPlainTextEdit *quote_text = new QPlainTextEdit;
    quote_text->setReadOnly(true);
    quote_text->setMinimumWidth(600);
    quote_text->setMaximumWidth(600);
    quote_text->setMinimumHeight(300);
    quote_text->setMaximumHeight(300);
    
    layout->addWidget(quote_text);
    quoteLayout->setLayout(layout);
    

    }

    void Topcash::createFinalArea(){
    finalLayout = new QGroupBox(tr("3) Choose Action:"));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->setAlignment(Qt::AlignLeft);
    
    QPushButton *export_csv = new QPushButton(tr("Export as CSV"));
    export_csv->setMaximumWidth(298);
    export_csv->setMinimumWidth(298);
    
    QPushButton *email_list = new QPushButton(tr("Email List"));
    email_list->setMaximumWidth(298);
    email_list->setMinimumWidth(298);
    
    layout->addWidget(export_csv);
    layout->addWidget(email_list);
    
    finalLayout->setLayout(layout);
    

    }

    void Topcash::open(){
    QString file_name = QFileDialog::getOpenFileName(this);
    if (!file_name.isEmpty()){
    loadFile(file_name);
    }
    }

    void Topcash::loadFile(const QString &file_name){
    upload_box->setText("WHY IS THIS CRASHING!?!");

    }
    @

    topcash.h
    @#ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QLabel>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QGroupBox>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QLineEdit>
    #include <QPlainTextEdit>
    #include <QFileDialog>

    class Topcash : public QWidget{
    Q_OBJECT

    public:
    Topcash();

    private slots:
    void open();

    private:
    QGroupBox *uploadLayout, *finalLayout, *quoteLayout;
    QPushButton *upload_button, *browse_btn, *export_csv, *email_list;
    QLineEdit *upload_box;
    void createUploadArea();
    void createQuoteArea();
    void createFinalArea();
    void loadFile(const QString &file_name);
    };

    #endif // WIDGET_H
    @

    For some reason that I can't figure out, the app crashes when loadFile is called. Any ideas?
    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aburritojr
      wrote on last edited by
      #2

      Also, program crashes whenever I add variables (not pointers) to the header file. What is going on???

      1 Reply Last reply
      0
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #3

        The reason is that you re-declare member variables in the construction methods.
        You have:
        @
        private:
        QGroupBox *uploadLayout, *finalLayout, *quoteLayout;
        QPushButton *upload_button, *browse_btn, *export_csv, *email_list;
        QLineEdit *upload_box;
        @
        But then you do this:
        @
        QPushButton *browse_btn = new QPushButton(tr("Browse"));
        browse_btn->setMaximumWidth(80);
        @

        So you are not setting the class variable browse_btn.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aburritojr
          wrote on last edited by
          #4

          Ah of course thank you. It's been a while since I've written C++ and I had a feeling it was something stupid like that which was tripping me up.

          Any ideas why it's segfaulting when I add non pointers to the header file. For example, the line "QString why_is_this_crashing;" added to the private section of my class file causes my app to crash. Am I missing an include or something?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            Hi, missing an include or something, will cause compile error or link error. But what you encountered is running error.

            Hi, in normal cases, application crash can be easily solved is you can re-produced it. Simply debug your application you can find which line cause the crash.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aburritojr
              wrote on last edited by
              #6

              I'm running this code in Qt Creator:
              @#include "topcash.h"

              Topcash::Topcash(){
              createUploadArea();
              createQuoteArea();
              createFinalArea();

              QVBoxLayout *mainLayout = new QVBoxLayout;
              mainLayout->addWidget(uploadLayout);
              mainLayout->addWidget(quoteLayout);
              mainLayout->addWidget(finalLayout);
              setLayout(mainLayout);
              

              }

              void Topcash::createUploadArea(){
              uploadLayout = new QGroupBox(tr("1) Select file to upload"));
              QVBoxLayout *layout = new QVBoxLayout;

              QGroupBox *upload_horiz = new QGroupBox();
              QHBoxLayout *upload_layout = new QHBoxLayout;
              upload_layout->setAlignment(Qt::AlignLeft);
              
              upload_box = new QLineEdit;
              upload_box->setMinimumWidth(400);
              upload_box->setMaximumWidth(400);
              upload_box->setReadOnly(true);
              upload_box->setStyleSheet("background-color:#D8D8D8;border:1px solid #B0B0B0;border-radius:3px;");
              
              browse_btn = new QPushButton(tr("Browse"));
              browse_btn->setMaximumWidth(80);
              connect(browse_btn, SIGNAL(clicked()), this, SLOT(open()));
              
              upload_layout->addWidget(upload_box);
              upload_layout->addWidget(browse_btn);
              upload_horiz->setLayout(upload_layout);
              
              upload_button = new QPushButton(tr("Upload"));
              upload_button->setMaximumWidth(80);
              
              layout->addWidget(upload_horiz);
              layout->addWidget(upload_button);
              uploadLayout->setLayout(layout);
              

              }

              void Topcash::createQuoteArea(){
              quoteLayout = new QGroupBox(tr("2) Quote List:"));

              QVBoxLayout *layout = new QVBoxLayout;
              layout->setAlignment(Qt::AlignTop);
              
              QPlainTextEdit *quote_text = new QPlainTextEdit;
              quote_text->setReadOnly(true);
              quote_text->setMinimumWidth(600);
              quote_text->setMaximumWidth(600);
              quote_text->setMinimumHeight(300);
              quote_text->setMaximumHeight(300);
              
              layout->addWidget(quote_text);
              quoteLayout->setLayout(layout);
              

              }

              void Topcash::createFinalArea(){
              finalLayout = new QGroupBox(tr("3) Choose Action:"));

              QHBoxLayout *layout = new QHBoxLayout;
              layout->setAlignment(Qt::AlignLeft);
              
              export_csv = new QPushButton(tr("Export as CSV"));
              export_csv->setMaximumWidth(298);
              export_csv->setMinimumWidth(298);
              
              email_list = new QPushButton(tr("Email List"));
              email_list->setMaximumWidth(298);
              email_list->setMinimumWidth(298);
              
              layout->addWidget(export_csv);
              layout->addWidget(email_list);
              
              finalLayout->setLayout(layout);
              

              }

              void Topcash::open(){
              //QString file_name = QFileDialog::getOpenFileName(this);
              //upload_box->setText(file_name);
              //upload_file = file_name;
              //if (!file_name.isEmpty()){
              // loadFile(file_name);
              //}
              }

              void Topcash::loadFile(const QString &file_name){
              /*
              //upload_box->setText("WHY IS THIS CRASHING!?!");
              QFile file(file_name);
              if (!file.open(QFile::ReadOnly | QFile::Text)) {
              QMessageBox::warning(this, file_name,
              tr("Cannot read file %1:\n%2.")
              .arg(file_name)
              .arg(file.errorString()));
              return;
              }

              QTextStream in(&file);*/
              

              }
              @

              And it tells me I'm getting a segfault on line 26.

              When I compile it myself with qmake, it runs perfectly fine. Is this a bug in Qt Creator?

              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