qt 5.8 - > 5.9 logic_error

时间:2017-06-13 12:18:47

标签: c++ qt c++11

最近从qt 5.8更新到qt 5.9,我遇到了一些问题。

按下未连接到任何插槽的按钮,我会发现:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

这很奇怪,因为在更新之前,它完美无缺。我将粘贴代码的某些部分,以便更清晰。

首先,GUI是一个继承自QWidget的类。

#ifndef GUI_H
#define GUI_H

#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include "Controller.h"

class GUI : public QWidget
{
    Q_OBJECT
public:
    GUI(Controller* ctrl);
    void    start();
    ~GUI();
private:
    QLineEdit* txt_title;
    QLineEdit* txt_desc;
    QLineEdit* txt_type;
    QLineEdit* txt_dur;
    QPushButton* add_btn;
    QPushButton* upd_btn;
    QPushButton* rm_btn;
    QPushButton* fil_desc_btn;
    QPushButton* fil_type_btn;
    QPushButton* fil_search_btn;
    QPushButton* sort_title_btn;
    QPushButton* sort_desc_btn;
    QPushButton* sort_type_btn;
    QPushButton* undo_btn;
    QPushButton* refresh_btn;
    QPushButton* shuffle_btn;
    QListWidget* activities;
    Controller* ctrl;
    void    setup_gui();
    void    connect_gui();
    void    add_to_list(Activity&);
    void    update_list();

public slots:
    void    add();
    void    edit();
    void    remove();
    void    search();
    void    undo();
    void    refresh_list();
    void    sort_title();
    void    sort_desc();
    void    sort_type();
    void    filter_desc();
    void    filter_type();
    void    shuffle();
};

#endif

相关方法:

GUI::GUI(Controller* _ctrl)
{
    this->ctrl = _ctrl;
    this->setup_gui();
    this->connect_gui();
    setAttribute(Qt::WA_DeleteOnClose);
}

void    GUI::add_to_list(Activity& activity)
{
    string  str = "";

    str += activity.get_title() + "; " + activity.get_description() + "; " + activity.get_type() + "; " + std::to_string(activity.get_duration());

    new QListWidgetItem(QString::fromStdString(str), this->activities);
}

void    GUI::setup_gui()
{
    this->setContentsMargins(10, 10, 10, 10);
    QHBoxLayout* main_lay = new QHBoxLayout(this);
    QVBoxLayout* left_lay = new QVBoxLayout();
    QVBoxLayout* right_lay = new QVBoxLayout();


    QHBoxLayout* title_lay = new QHBoxLayout();
    QLabel* lbl_title = new QLabel(QObject::tr("title"));
    lbl_title->setAlignment(Qt::AlignCenter);
    this->txt_title = new QLineEdit();

    QHBoxLayout* desc_lay = new QHBoxLayout();
    QLabel* lbl_desc = new QLabel(QObject::tr("description"));
    lbl_desc->setAlignment(Qt::AlignCenter);
    this->txt_desc = new QLineEdit();

    QHBoxLayout* type_lay = new QHBoxLayout();
    QLabel* lbl_type = new QLabel(QObject::tr("type"));
    lbl_type->setAlignment(Qt::AlignCenter);
    this->txt_type = new QLineEdit();

    QHBoxLayout* dur_lay = new QHBoxLayout();
    QLabel* lbl_dur = new QLabel(QObject::tr("duration"));
    lbl_dur->setAlignment(Qt::AlignCenter);
    this->txt_dur = new QLineEdit();

    QHBoxLayout* op_lay = new QHBoxLayout();
    this->add_btn = new QPushButton(QObject::tr("&Add"));
    this->upd_btn = new QPushButton(QObject::tr("Updat&e"));
    this->rm_btn = new QPushButton(QObject::tr("&Remove"));

    QHBoxLayout* fil_lay = new QHBoxLayout();
    this->fil_desc_btn = new QPushButton(QObject::tr("FilterBy&Desc"));
    this->fil_type_btn = new QPushButton(QObject::tr("FilterBy&Type"));
    this->fil_search_btn = new QPushButton(QObject::tr("&Search"));

    QHBoxLayout* undo_lay = new QHBoxLayout();
    this->undo_btn = new QPushButton(QObject::tr("U&ndo"));
    this->shuffle_btn = new QPushButton(QObject::tr("S&huffle"));
    this->refresh_btn = new QPushButton(QObject::tr("Re&fresh"));

    this->activities = new QListWidget();

    main_lay->addLayout(left_lay);
    main_lay->addLayout(right_lay);

    title_lay->addWidget(lbl_title);
    title_lay->addStretch();
    title_lay->addWidget(txt_title);
    desc_lay->addWidget(lbl_desc);
    desc_lay->addStretch();
    desc_lay->addWidget(txt_desc);
    type_lay->addWidget(lbl_type);
    type_lay->addStretch();
    type_lay->addWidget(txt_type);
    dur_lay->addWidget(lbl_dur);
    dur_lay->addStretch();
    dur_lay->addWidget(txt_dur);
    op_lay->addWidget(add_btn);
    op_lay->addWidget(upd_btn);
    op_lay->addWidget(rm_btn);
    fil_lay->addWidget(fil_desc_btn);
    fil_lay->addWidget(fil_type_btn);
    fil_lay->addWidget(fil_search_btn);
    undo_lay->addWidget(refresh_btn);
    undo_lay->addWidget(shuffle_btn);
    undo_lay->addWidget(undo_btn);

    right_lay->addLayout(title_lay);
    right_lay->addLayout(desc_lay);
    right_lay->addLayout(type_lay);
    right_lay->addLayout(dur_lay);
    right_lay->addLayout(op_lay);
    right_lay->addLayout(fil_lay);
    right_lay->addLayout(undo_lay);

    QHBoxLayout* sort_lay = new QHBoxLayout();
    this->sort_title_btn = new QPushButton(QObject::tr("SortByTit&le"));
    this->sort_desc_btn = new QPushButton(QObject::tr("SortByDes&c"));
    this->sort_type_btn = new QPushButton(QObject::tr("SortByT&ype"));

    left_lay->addWidget(activities);
    left_lay->addLayout(sort_lay);

    sort_lay->addWidget(sort_title_btn);
    sort_lay->addWidget(sort_desc_btn);
    sort_lay->addWidget(sort_type_btn);

    this->refresh_list();
}

void    GUI::connect_gui()
{
    QObject::connect(add_btn, SIGNAL(clicked()), this, SLOT(add()));
    QObject::connect(upd_btn, SIGNAL(clicked()), this, SLOT(edit()));
    QObject::connect(rm_btn, SIGNAL(clicked()), this, SLOT(remove()));
    QObject::connect(sort_title_btn, SIGNAL(clicked()), this, SLOT(search()));
    QObject::connect(undo_btn, SIGNAL(clicked()), this, SLOT(undo()));
    QObject::connect(refresh_btn, SIGNAL(clicked()), this, SLOT(refresh_list()));
    QObject::connect(fil_search_btn, SIGNAL(clicked()), this, SLOT(search()));
    // QObject::connect(sort_title_btn, SIGNAL(clicked()), this, SLOT(sort_title()));
    QObject::connect(sort_type_btn, SIGNAL(clicked()), this, SLOT(sort_type()));
    QObject::connect(sort_desc_btn, SIGNAL(clicked()), this, SLOT(sort_desc()));
    QObject::connect(shuffle_btn, SIGNAL(clicked()), this, SLOT(shuffle()));
    QObject::connect(fil_desc_btn, SIGNAL(clicked()), this, SLOT(filter_desc()));
    QObject::connect(fil_type_btn, SIGNAL(clicked()), this, SLOT(filter_type()));
}

主要问题是按钮&#34; sort_title_btn&#34;当连接到空方法或根本没有连接时,单击时会出现错误。

项目的创建是通过以下方式完成的:

qmake -project

编辑.pro文件以添加QT += core gui widgets并包含带标题的文件夹。

qmake

最后:make

很好地编译,所有按钮都可以工作,除了&#34; sort_title_btn&#34;即使没有连接到SLOT和&#34;搜索&#34;如果必须进行搜索的字段为空,则可以轻松修复该问题。

那么,&#34; sort_title_btn&#34;有什么需要做的?

我试着把它放到另一个SLOT上,它有效。更改我连接它的函数的名称似乎错误或给我错误的东西。

0 个答案:

没有答案