不使用线程的 C++/Qt 线程错误

C++/Qt Threads error without using threads

我用的是特殊形式的qt框架,看起来会有点复杂。不过别担心。和qt原来的框架是一样的。 (不,我不能改变它)

我的这个程序的目的是从 ui 加载 tableview 中的 table。 tableview 应具有与目录中的文件一样多的行和文本。我获取目录中的所有文件并对它们进行过滤。只有选定的一个才被计算在内并在以后使用。

现在我的问题是: 我想像 Excel table 一样显示 table:它应该突出显示我的光标所在的单元格(在我的示例中它是一个上下移动的计数器)现在(当前我没有光标,所以我必须使用 self-created "cursor"。你会在程序中看到我的意思)。 我使用 QStandardItemModel 来显示它,并使用 QStandardItem 来定义它。 因为我想更改单元格的颜色,所以我使用 QStandardItem 数组来一个一个地保存单元格。

form.h:

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QTextTable>
#include <QFont>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QString>
#include <QStringList>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QBrush>


namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

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

    void my_setCFGFromDirectory();
    void my_Highlighted(int, bool);
    void my_loadCFG(int);


private:
    Ui::Form *ui;

    QFile file;
    QTextStream textstream;
    QDir directory;
    QString filename;
    QStringList stringlist;
    QStringList filter;
    QStandardItemModel *model;
    QStandardItem *tableitem;
    QStandardItem* pointerTableitem[];

    int max_files;
    int w_counter;
    bool check1;
    bool check2;
    bool check3;
};

#endif // FORM_H

和 form.cpp

#include "form.h"
#include "ui_form.h"
#include "rbobject.h"

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

Form::~Form()
{
    delete ui;
}

void Form::my_Highlighted(int actual_count, bool modus){
    if(modus == 1){
        w_counter = 0;
        while(w_counter < max_files){
            if(w_counter == actual_count){
                pointerTableitem[w_counter]->setBackground(QBrush(QColor(130,180,255)));
            }
            w_counter++;
        }
    }
    else
    {
        w_counter = 0;
        while(w_counter < max_files){
            pointerTableitem[w_counter]->setBackground(QBrush(QColor(255,255,255)));
            w_counter++;
        }
    }
}



void Form::my_setCFGFromDirectory(){
    directory.setPath("/etc/rosenbauer/CFG-Files");
    filter << "*_CFG.ini";
    stringlist = directory.entryList(filter);      //takes the selected files and wriths them in a filelist
    max_files = stringlist.count();               //Counts the selected files
    pointerTableitem [max_files];
    memset(pointerTableitem, 0, max_files*sizeof(int)); //The compiler can not see how many elements should be in the array at the time he constructs the array.
                                                        //So you have to use this to tell the compiler, how many elements it should create

    model = new QStandardItemModel(max_files, 1, this);             //Rows , Columns , this
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("CFG-Files"))); //Column , QStandarditem...name

    for(w_counter = 0; w_counter != max_files; w_counter++){
        tableitem = new QStandardItem();
        tableitem->setBackground(QBrush(QColor(255,255,255)));
        tableitem->setText(QString(stringlist[w_counter]));     //Wriths text in the cell
        qDebug() << tableitem->text();

        pointerTableitem[w_counter] = tableitem;    //Stacks the tableitems in a filelist
        model->setItem(w_counter, 0, tableitem);    //Row, Column , tableitem...text
    }
    w_counter = 0;

    //pointerTableitem[2]->setBackground(QBrush(QColor(130,180,255))); //Test

    ui->TableConfig->setModel(model);   //Sets the table into the UI
}



void Form::my_loadCFG(int file_position){


    check1 = 0;
    check2 = 0;

    directory.setPath("/etc/rosenbauer/etc/rosenbauer");
    check1 = directory.remove("SA008_890560-001_CFG.ini");
    check2 = directory.remove("reparsed/SA008_890560-001_CFG.ini_reparsed");

    if(check1 && check2){
        filename = pointerTableitem[file_position]->text();
        check3 = QFile::copy("/etc/rosenbauer/CFG-Files/"+filename, "/etc/rosenbauer/SA008_890560-001_CFG.ini");

        if(!check3){
            qDebug() << "Error! Could not copy new CFG-file into directory";
        }
    }
    else
    {
        qDebug() << "Error! Could not delete running CFG-file(s) from directory";
    }


}

主要headerrbobject45000.h

#ifndef RbObject45000_H
#define RbObject45000_H

#include "rbobject.h"
#include "form.h"
class RbObject45000 : public RbObject{


public:
    RbObject45000();
    RbObject45000(qint32 rbObjectId, RDataObject *dataobject, qint32 style, QObject *parent = 0);
    RbObject45000(qint32 rbObjectId, qint32 style, qint32 prio, QObject *parent);

    bool entered_ui;
    bool entered_key;
    short counter;

    short w_count;
    short delay_count;

    short max_files;
    short begin;
    short end;

    Form *f;



    void exec();

~RbObject45000();
};

#endif // RbObject45000_H

这是主要的: rbobject45000.cpp

RbObject45000::RbObject45000(qint32 rbObjectId, qint32 style, qint32 prio, QObject *parent):
    RbObject(rbObjectId, style, prio, parent){

    entered_ui = 0;
    entered_key = 0;

    counter = 0;
    w_count = 1; //... whilecounter
    delay_count = 0; //... delay for the deadtime

    max_files = 0;
    begin = 0;
    end = 0;

    f= new Form(); //f...name of the displayed UI
}


void RbObject45000::exec(){

//Opens the file on the display
    if(getKey(RB_KEY_9) && getKey(RB_KEY_7) && entered_ui ==0){
        f->show();
        entered_ui = 1;


        // Geting the Files from Directory
        f->my_setCFGFromDirectory();
    }


// Prescind them file in the table
    if(entered_ui == 1 && delay_count == 2){

        if(getKey(RB_KEY_7)){
            counter--;
            entered_key = 1;
            if(counter < 0){
                counter = 0;
            }
        }
        else if(getKey(RB_KEY_9)){
            counter++;
            entered_key = 1;
            if(counter > max_files){
                counter = max_files;
            }
        }


        if(entered_key == 1){
            while(w_count <= max_files){
                f->my_Highlighted(w_count, 0); //Cell , not highlighted
                w_count++;
            }
            w_count = 0;
            f->my_Highlighted(counter,1);  //Cell , highlighted
            entered_key = 0;
        }
        delay_count = 0;

    }
    else if(delay_count == 2){  //if delay == 2 and nobody hit a button
        delay_count = 0;
    }
    delay_count++;


    //Load the coosen file as programm-config in
    if(entered_ui == 1){
        if(getKey(RB_KEY_8)){
           f->my_loadCFG(counter);
        }
    }

}

(RB_KEY_7-9是一个hardware-module的按钮,只需要点击它们) 所以如果我这样做,程序将编译并启动。如果我按下按钮,程序 运行 进入 my_setCFGFromDirectory() 它退出 window 并停止 运行ning,但是如果 QStandardItem pointerTableitemmy_setCFGFromDirectory() 一切正常(但 pointerTableitem 需要 private)。

我得到的错误是:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is Form(0x167498), parent's thread is QThread(0x1414e8), current thread is QThread(0x15d2d8)
Segmentation fault

似乎说我使用了线程(我确定我没有)。但我知道它与 pointerTableitem 以及 header 和 cpp.

中的声明有关

如果我没记错的话,问题出在这里:

model = new QStandardItemModel(max_files, 1, this);

您尝试创建一个 QStandardItemModel 将您的 Form 实例 this 作为父级传递,表单是在主线程上创建的,这个调用显然发生在另一个线程上(您所做的编辑还不够,目前还不清楚 RbObject 是否继承了 QThread 但您可以自己检查)以及错误消息。

为什么不在主线程调用函数?而不是这样做

f->show();
entered_ui = 1;

// Geting the Files from Directory
f->my_setCFGFromDirectory();

在另一个线程中,为 Form class 创建一个 slot/signal 对,将此代码放入槽中并触发来自 RbObject45000::exec() 的连接信号。从非 UI trhead 进行 UI 操作是不正确的。