不使用线程

时间:2015-07-21 09:22:31

标签: c++ qt uitableview user-interface qt4

我正在使用qt框架的一种特殊形式,它看起来有点复杂。不过别担心。它与qt的原始框架相同。 (不,我不能改变它)

我对此程序的意图是从ui加载tableview的表。 tableview应包含与目录中的文件一样多的行和文本。我获取目录中的所有文件并对它们进行过滤。只计算选定的一个并在之后使用。

现在我的问题: 我想像Excel表一样显示表格:它应该突出显示我的光标(在我的例子中是一个上下计数器)的单元格(目前我没有光标,所以我必须使用一个自创的“光标”。你会在程序中看到我的意思)。 我使用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";
    }


}

主标题rbobject45000.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是硬件模块的按钮,只需点击它们) 所以,如果我这样做,程序将编译并启动。如果我按下按钮并且程序运行到my_setCFGFromDirectory()它退出窗口并停止运行,但如果在QStandardItem pointerTableitem中声明了my_setCFGFromDirectory(),一切正常(但是{{1}需要是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 以及标题和cpp中的声明有关。

1 个答案:

答案 0 :(得分:0)

如果我没弄错,问题出在这里:

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

您尝试创建QStandardItemModelForm实例this作为父级传递,表单是在主线程上创建的,此调用显然发生在另一个线程上(编辑您如果RbObject继承QThread但你可以检查自己),那么仍然不清楚,因此仍然不清楚错误信息。

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

f->show();
entered_ui = 1;

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

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