2013-08-19 26 views
0

我有一個簡單的gui,它有一個文本字段,一個下拉菜單和一個go按鈕。我可以指定我正在查找的零件的名稱和類別,並通過將「go」按鈕連接到一個運行我已經創建的功能的插槽來調用一個函數。Qt C++函數中斷刪除未知變量

但是,當插槽功能完成所有事情時,它會調用xstring中的函數,這會刪除一些大量的xstring。它道出了這樣的功能:

void _Tidy(bool _Built = false, 
    size_type _Newsize = 0) 
    { // initialize buffer, deallocating any storage 
    if (!_Built) 
     ; 
    else if (this->_BUF_SIZE <= this->_Myres) 
     { // copy any leftovers to small buffer and deallocate 
     pointer _Ptr = this->_Bx._Ptr; 
     this->_Getal().destroy(&this->_Bx._Ptr); 
     if (0 < _Newsize) 
      _Traits::copy(this->_Bx._Buf, 
       _STD addressof(*_Ptr), _Newsize); 
     this->_Getal().deallocate(_Ptr, this->_Myres + 1); 
     } 
    this->_Myres = this->_BUF_SIZE - 1; 
    _Eos(_Newsize); 
} 

而我的程序執行,在this->_Getal().deallocate(_Ptr, this->_Myres + 1);休息。

這裏的GUI界面代碼:

#include <QtGui> 
#include <QApplication> 
#include <QComboBox> 
#include "gui.h" 
#include <vector> 

std::vector<std::string> PartClasses; 

gui::gui(QWidget *parent) : QDialog(parent){ 

    getPartClasses(PartClasses); //my own function, does not affect how the gui runs, just puts strings in PartClasses 

    label1 = new QLabel(tr("Insert Name (Optional):")); 
    label2 = new QLabel(tr("Class Name (Required):")); 
    lineEdit = new QLineEdit; 

    goButton = new QPushButton(tr("&Go")); 
    goButton->setDefault(true); 
    connect(goButton, SIGNAL(clicked()), this, SLOT(on_go_clicked())); 

    cb = new QComboBox(); 
    for(int i = 0; i < PartClasses.size(); i++) 
     cb->addItem(QString::fromStdString(PartClasses[i])); 

    //*add widgets to layouts, removed for space* 

    setWindowTitle(tr("TEST")); 
    setFixedHeight(sizeHint().height()); 
} 

void gui::on_go_clicked(){ 
    std::string str(cb->currentText().toStdString()); 
    updateDB(str, lineEdit->text().toUtf8().constData()); //my function, does not affect the gui. 
    QApplication::exit(); 
} 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    gui *stuff = new gui; 
    stuff->show(); 
    return app.exec(); 
} 

它正在做什麼?當我完成插槽時,gui是不是應該回來,以便我可以指定一個新對象?我怎樣才能讓它不要刪除這個對象,或讓它成功?

+3

聲音就像你在程序中崩潰一樣,與信號和插槽完全無關 – Chris

+0

我只是在'on_go_clicked()'函數調用函數後添加'QApplication :: exit()',並且它執行退出,但gui沒有關閉,程序仍然中斷。 –

+0

克里斯,功能運行後,我的程序中不應該發生任何事情。我寫了沒有函數的gui,並且它工作。我沒有gui寫了這個函數,它起作用了。我把函數放在gui中,它調用函數,完成並返回到gui,但當插槽函數完成時會崩潰。 –

回答

0

這裏是一個正在發生的事情我最好的猜測:

您訪問它不應該是取得了一些刪除的對象。

_Tidy函數看起來像它正在做一些清理後的字符串操作。機會是你的char *的不變性,並且你正在刪除一個const指針。

要解決這個問題,我會做一個變量的深層副本,並將其傳遞到updateDBxstring LaTex中。或者你可以創建一個xstring對象,然後傳遞給它。

我也會考慮使用strcpy或類似的東西,或者只是std::string

此外,出現的崩潰代碼也可能有幫助。

編輯:

這裏是你的代碼也許應該看起來像......

void gui::on_go_clicked(){ 
    std::string str(cb->currentText().toStdString()); 
    std::string line_edit_str(lineEdit->text().toUtf8().constData()); 

    updateDB(str, line_edit_str); //my function, does not affect the gui. 
    QApplication::exit(); 
} 

希望有所幫助。

+0

由於某些原因,這是因爲我使用'toStdString()'而不是'.toUtf8()。constData()'。我討厭它不會讓我以某種原因使用正常的方式。 –

+0

QString功能非常強大,可以處理UTF,寬字符和互聯網標準的所有本地化瘋狂。如果將變量放入本地作用域變量中,則不應該有問題,但如果傳遞不可變的const元素,則會出現問題。 http://stackoverflow.com/questions/4214369/how-to-convert-qstring-to-stdstring – phyatt

+0

http://stackoverflow.com/questions/12495226/immutable-object-in-collections-c-and-qt – phyatt