PDA

View Full Version : correct way to share data between 2 UI classes ?



newbie1
22nd August 2011, 14:27
Hello,
I have 2 forms created with QT Creator ( example MainWindow and an Importdialog ). Both have a treewidget and I would like to be able to copy treewidgetitems from the Import dialog to the mainwindow. How do I make the items I copy in the import dialog available to the MainWindow ( scope ? ) ?
Thank you
newbie

high_flyer
22nd August 2011, 14:35
Do you know how to populate a QTreeWidget?

newbie1
22nd August 2011, 14:43
Hi,
I hope so :-). The following is a SLOT in the Import dialog:


QTreeWidgetItem *c_item= new QTreeWidgetItem();
if (ui->treeWidget->selectedItems().length()==1) {
c_item=ui->treeWidget->selectedItems().at(0);
}
The problem is that in this Ui class the MainWindow is not visible/not in scope, so I cannot simply do MainWindow::insertmywidget (c_item ).

Thank you

high_flyer
22nd August 2011, 14:52
no, this code does not populate the tree.
This code gets a selected item from the tree if only one item is selected.
So this means you don't know how to populate a three.
Start there.
Learn how a tree is populated, this will help you then with the question you posted.
Post again if you still need clarifications.

newbie1
22nd August 2011, 14:55
Must have misunderstood your question ...

In the Mainwindow I add treewidgetitems like this for example:

QTreeWidgetItem *qtr=ui->treeWidget->currentItem();
if (qtr != NULL ){
QTreeWidgetItem *cities = new QTreeWidgetItem(qtr);
cities->setText(0, ui->lineEdit_2->text()); // Description
QString b=ui->lineEdit_3->text(); // lat/long
b.append("/");

etc.

high_flyer
22nd August 2011, 15:01
The problem is that in this Ui class the MainWindow is not visible/not in scope, so I cannot simply do MainWindow::insertmywidget (c_item ).
Why not?
Visible does not mean it does not exist in memory.
Your problem is not so clear.
Please explain in more details exactly what you are trying to achieve this will help increase the quality of the help you'll receive.

newbie1
22nd August 2011, 15:20
Hi again,
1. Created with QT creator an application with a mainwindow and another window which is created based on a action in the MainWindow ( Button "import data" ) -> works fine
2. MainWindow w is instantiated in main.cpp ( default way how QTcreator does that )
3. I click the "import data" button in the mainwindow, open file selector dialog, read my csv file, display the contents in a treewidget -> all OK
3. then I select treewidgetitems in that treewidget , select "copy button" but when handling this signal in a slot in the import dialog class, the MainWindow seems not accessible.
I can't call any method/slot in the Mainwindow class. I assume this is because Mainwindow w is not in scope since it was declared in main.cpp and not in importclass.cpp. How do I make
it accessible ?

Thank you

high_flyer
22nd August 2011, 15:35
if both the main window and you copy dialog are in the main() scope, you can make the connection there.
But it would be semantically more correct to make the copy dialog a child of your main window, this way you also haver direct access to both widgets.