Updating QLineEdit Text Causing Crash



  • 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!



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



  • 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.



  • 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?



  • 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.



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.