How to retriew data from one ui to other ui in the same project.



  • Actually i hav mainwindow.ui and view.ui two ui's in my project.
    now i want read data from one of the qtablewidget data of mainwindow.ui from view.cpp
    so how can i reffer to mainwidow.ui object in view.cpp.
    please help.



  • Hi @moyin
    Can you specify where are the objects created?
    Either you can communicate using signal & slots between mainwindow & view object.



  • @yuvaram
    hi thnks for rply,
    i hav all objects in mainwindow and view is having only a tableview where i need to push the data extracted from mainwindow gui.
    please rply ASAP



  • @moyin
    use signal & slots to communicate
    i mean, emit signal from view to mainwindow.
    If provide ur code, its easy to show.



  • #include "myview.h"
    #include "ui_myview.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    //#include "view.h"
    #include <QStandardItemModel>
    #include <vector>
    #include "JailHouseCellList.h"
    #include "JailHouseCell.h"
    #include "MemRegion.h"
    #include "IrqChip.h"
    #include "PciDevice.h"
    #include <iostream>
    #include <QBrush>

    using namespace std;

    Myview::Myview(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Myview)
    {
    ui->setupUi(this);
    mwin = new Ui::MainWindow ;

    model->setHorizontalHeaderItem(0, new QStandardItem(QString("CELL")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("CPU")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("MEM REGIONS")));
    model->setHorizontalHeaderItem(3, new QStandardItem(QString("PCI DEVICES")));
    model->setHorizontalHeaderItem(4, new QStandardItem(QString("IRQ CHIPS")));
    //model->horizontalHeaderItem(2)->setText("Apple");
     ui->tableView->setModel(model);
    

    // tableView->setHidden(false);
    for (int i = 0; i < 5; i++)
    ui->tableView->setColumnWidth(i,200);
    ```
    mwin->lineEdit->setText("Root");
    JailHouseCell j (mwin->lineEdit->text()) ;

      //   JailHouseCell j("Root");
         JailHouseCell j1("redhawk") ;
         JailHouseCell j2("SEL4") ;
         PciDevice Y("P");
         PciDevice Z("q","z",12);
         MemRegion mz1("m1",1234,1234,39,"qwer|qw");
         MemRegion mz2("m2",1290,1234,39,"qwer|qw");
         std::vector<unsigned long> vec{123,345,456};
         IrqChip iq("iq1",2336655556,555,vec);
         IrqChip iq1("iq2",2336655557,555,vec);
         j.add_memRegion(mz1);
         j.add_memRegion(mz1);
         j.add_memRegion(mz2);
         j.add_irqChip(iq);
         j.add_irqChip(iq);
         j.add_irqChip(iq1);
         j.add_pci(Y);
         j1.add_pci(Z);
    
         j1.add_memRegion(mz1);
         j1.add_memRegion(mz1);
         j1.add_memRegion(mz2);
         j1.add_irqChip(iq);
         j1.add_irqChip(iq);
         j1.add_irqChip(iq1);
    
    
    
         std::vector<bool> x = {true,true,true,false,false,false};
         std::vector<bool> y = {false,false,false,true,true,false};
         std::vector<bool> z = {false,false,false,false,false,true};
         j.set_cpuIds(x);
         j1.set_cpuIds(y);
         j2.set_cpuIds(z);
    
    
         JailHouseCellList &  JL = JailHouseCellList::getJailHouseCellList();
         JL.add_cell(j);
         JL.add_cell(j1);
         JL.add_cell(j2);
        {
    
        int cell_row=0 , cpu_row=0 , mem_row=0, pci_row = 0, irq_row = 0;
      //  QString color[3] = {"red" , "green", "blue"} ;
          std::vector<Qt::GlobalColor> color = {Qt::red,Qt::gray, Qt::cyan, Qt::green};
    
         for( auto cell_itr = JL.get_JailHouseCellItr_begin() ;
                cell_itr != JL.get_JailHouseCellItr_end();
                ++cell_itr,++cell_row)
         {
             string cell_name = cell_itr->first;
             auto cell = cell_itr->second;
             QString q = QString::fromLocal8Bit(cell_name.c_str());
             cout << cell_name ;
             //      cout << q ;
             //autget_cpuIdso cell_info = (cell_itr->second);
    
             //QModelIndex index
             //          = model->index(row,0,QModelIndex());
             // 0 for all data
    
        //     QModelIndex index
        //         = model->index(cell_row,0,QModelIndex());
             // 0 for all data
      //       model->setData(index, q);
             auto item = new QStandardItem(q);
             item->setTextAlignment(Qt::AlignCenter);
    
    //         item->setForeground(Qt::red);
    
             item->setBackground(color[cell_row]);
             model->setItem(cpu_row,0,item);
    
             auto cpu_ids =  cell.get_cpuIds();
             for(auto & cpu_id : cpu_ids)
             {
               QString qcpuid = QString::number(cpu_id) ;
             //  QModelIndex index = model->index(cpu_row,1,QModelIndex());
            //   model->setData(index , qcpuid ) ;
               auto item = new QStandardItem(qcpuid);
               item->setTextAlignment(Qt::AlignCenter);
    
      //         item->setForeground(Qt::red);
    
               item->setBackground(color[cell_row]);
               model->setItem(cpu_row,1,item);
               ++cpu_row ;
             }
             for( auto mem_itr = cell.get_memRegionItr_begin();
                    mem_itr != cell.get_memRegionItr_end();
                    ++mem_itr,++mem_row)
             {
    
                 auto mem_name = (mem_itr->second).get_name();
                 QString q = QString::fromLocal8Bit(mem_name.c_str());
              //   QModelIndex index
              //       = model->index(mem_row,2,QModelIndex());
                 // 0 for all data
             //    model->setData(index, q);
                 auto item = new QStandardItem(q);
                 item->setTextAlignment(Qt::AlignCenter);
    
        //         item->setForeground(Qt::red);
    
                 item->setBackground(color[cell_row]);
                 model->setItem(mem_row,2,item);
    
             }
    
             for( auto pci_itr = cell.get_pciDeviceItr_begin();
                    pci_itr != cell.get_pciDeviceItr_end();
                    ++pci_itr,++pci_row)
             {
                 auto mem_name = (pci_itr->second).get_name();
                 QString q = QString::fromLocal8Bit(mem_name.c_str());
              //   QModelIndex index
              //       = model->index(pci_row,3,QModelIndex());
                 // 0 for all data
              //   model->setData(index, q);
                 auto item = new QStandardItem(q);
                 item->setTextAlignment(Qt::AlignCenter);
    
        //         item->setForeground(Qt::red);
    
                 item->setBackground(color[cell_row]);
                 model->setItem(pci_row,3,item);
    
             }
    
             for( auto irq_itr = cell.get_irqChipItr_begin();
                    irq_itr != cell.get_irqChipItr_end();
                    ++irq_itr,++irq_row)
             {
    
                 auto mem_name = (irq_itr->second).get_name();
                 QString q = QString::fromLocal8Bit(mem_name.c_str());
           //      QModelIndex index
            //           = model->index(irq_row,4,QModelIndex());
                 // 0 for all data
               //   QVariant Red(QColor(Qt::red));
            //     model->data(index, QVariant::fromValue(QColor(Qt::red))) ;
                 auto item = new QStandardItem(q);
                 item->setTextAlignment(Qt::AlignCenter);
    
       //         item->setForeground(Qt::red);
    
                 item->setBackground(Qt::red);
                 model->setItem(irq_row,4,item);
         //        model->setData(index, q);
    
                 //model->setData(Qt::ColorOnly Qt::BackgroundColorRole) ;
              //   QBrush redBackground(Qt::red);
               //  model->data(index, redBackground) ;
    
               //  model->data(index, Red) ;
    
             }
    
         }
         }
    
    }
    
    Myview::~Myview()
    {
        delete ui;
    }


  • @yuvaram

    this is my myview.cpp where i hav bolded text what i'm trying.


  • Moderators

    @moyin As @yuvaram already said use signals/slot to communicate between different UI parts. See http://doc.qt.io/qt-5.9/signalsandslots.html
    Your view should not know anything about main window.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.