简体   繁体   English

将对象向量传递到新的对话框窗口(Qt C ++)

[英]Passing vector of objects to new dialog window (Qt C++)

I've got very strange problem. 我有一个很奇怪的问题。

I've created class which contains few string and ints: 我创建了包含几个字符串和整数的类:

class muzyka
{
    string artist;
    string album;
    string genre;
    string type;
    string label;
    string cat_nr;
    int songs;
    int year;

public:

    muzyka();
    muzyka(string ar, string al, string g, string t, string l, string c, int s, int y);
    ~muzyka();

    void setartist(string ar);
    void setalbum(string al);
    void setgenre(string g);
    void settype(string t);
    void setlabel(string l);
    void setcat_nr(string c);
    void setsongs(int s);
    void setyear(int y);

    void setall(string ar, string al, string g, string t, string l, string c, int s, int y);

    string getartist();
    string getalbum();
    string getgenre();
    string gettype();
    string getlabel();
    string getcat_nr();
    int getsongs();
    int getyear();

};

I created a vector for storing this objects. 我创建了一个用于存储此对象的向量。 My problem is that I have 2 classes (MainWindow and Dialog). 我的问题是我有2个类(MainWindow和Dialog)。 In MainWindow I have QTableWidget which shows all records. 在MainWindow中,我有QTableWidget显示所有记录。 New records are adding in Dialog window which contains line edit boxes. 新记录将添加到包含行编辑框的“对话”窗口中。

This vector is created as a global variable in mainwindow.cpp (I know it's a bad habit) I created functions as a public methods in mainwindow and dialog classes to send and recieve my vector. 该向量在mainwindow.cpp中作为全局变量创建(我知道这是一个坏习惯),我在mainwindow和dialog类中创建了作为公用方法的函数来发送和接收我的向量。 It looks like this: 看起来像这样:

Dialog(recieves old vector, which was displayed on QTable 对话框(接收到旧的向量,该向量显示在QTable上

void add::recieve(QVector<muzyka> &db)
{
    cont = db;

}

MainWindow(recieves edited vector) MainWindow(已接收矢量)

void MainWindow::pass(QVector<muzyka> cont)
{
    db = cont;

}

part of dialog window.cpp looks like this: 对话框window.cpp的一部分看起来像这样:

cont.push_back(muzyka(ar, al, g, t, l, c, s, y));

MainWindow a;

a.pass(cont);

And in mainwindow.cpp I have refresh button: 在mainwindow.cpp中,我有刷新按钮:

void MainWindow::on_pushButton_clicked()
{

    ui->tableWidget->setRowCount(db.size());
    ui->tableWidget->setColumnCount(8);


   QVector<muzyka>::iterator i;

   for(i = db.begin(); i != db.end(); ++i)
   {
     for(int row = 0; row < db.size(); row++)
    {
         QString temp_1,temp_2,temp_3,temp_4,temp_5,temp_6;
         int temp1,temp2;

         temp_1=(*i).getartist().c_str();
         ui->tableWidget->setItem(row,0,new QTableWidgetItem(temp_1));

         temp_2=(*i).getalbum().c_str();
         ui->tableWidget->setItem(row,1,new QTableWidgetItem(temp_2));

         temp1=(*i).getsongs();
         ui->tableWidget->setItem(row,2,new QTableWidgetItem(temp1));

         temp2=(*i).getyear();
         ui->tableWidget->setItem(row,3,new QTableWidgetItem(temp2));

         temp_3=(*i).getgenre().c_str();
         ui->tableWidget->setItem(row,4,new QTableWidgetItem(temp_3));

         temp_4=(*i).getlabel().c_str();
         ui->tableWidget->setItem(row,5,new QTableWidgetItem(temp_4));

         temp_5=(*i).gettype().c_str();
         ui->tableWidget->setItem(row,6,new QTableWidgetItem(temp_5));

         temp_6=(*i).getcat_nr().c_str();
         ui->tableWidget->setItem(row,7,new QTableWidgetItem(temp_6));

     }

}

When I add first record and press refresh button in QTable I see my data(not exacly quite good but it's not problem for now). 当我添加第一条记录并在QTable中按“刷新”按钮时,我看到了我的数据(虽然不是很好,但是现在这不是问题)。 But when I try to add next record after refreshing I see two rows of the second object. 但是,当我尝试在刷新后添加下一条记录时,我看到第二个对象的两行。 I have really no idea what's wrong. 我真的不知道怎么了。 I guess it may be something with passing vector to windows. 我猜可能是将向量传递给Windows的原因。

Do you have any solution for this? 您对此有什么解决方案吗?

I will be really thankful for any help. 我将非常感谢您的帮助。

您只需要删除第一个循环,然后使用db.at(row)代替(* i)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM