PDA

View Full Version : Add Item in other Class



Nazgul
11th April 2011, 20:53
Hello,

My problem is that I've got a class, which has a few values which need to be at the Listview, I know how to do this if the value is inside the class itself.

But my ui struct is inside another class, so how can I pass the value to it?

I tried to make a function in the class which has the ui and call it, but it did notw ork , but the weird thing is that when I aclled the same function from the ui class it did work?

well if someone has a better solution, please

Regards

TomJoad
12th April 2011, 00:42
For the ui class, are you talking about something along these lines:


foo()
{
Ui::MainWindow mw;
QString str = mw.textEdit->toPlaintText();
}

or


foo()
{
MainWindow mw;
QString str = mw.foo2();
}

QString MainWindow::foo2()
{
QString str = ui.textEdit->toPlaintText();
return str
}

and then it doesn't assign str correctly? If so, I have the same problem, and would also like to know how to fix it.

ChrisW67
12th April 2011, 01:21
I tried to make a function in the class which has the ui and call it, but it did notw ork , but the weird thing is that when I aclled the same function from the ui class it did work?
How did it not work? Failed to compile, failed to link, did nothing when called, did something completely bizarre when called, crashed the program when called...

We are not mind readers. Shows snippets of code where you declare the receiving method, how you call them, and explain what you expected and why your expectation was not met.

Added after 21 minutes:


and then it doesn't assign str correctly? If so, I have the same problem, and would also like to know how to fix it.
In both foo() functions the variable str is local and destroyed at the end of the function. Str will be set but nothing is done with either variable before it is destroyed.

Your foo2() method will return the plain text content of the text edit (assuming you fix the typos). Since you are passing a default constructed main window in your examples this will almost certainly be an empty string.

What were you expecting?

TomJoad
12th April 2011, 02:27
ChrisW67:

I assume you are doing something with str after.

For the first, it compiles but leaves the debug message of "Segmentation fault by signal SIGSEGV" at line 190 of QTextEdit.h, which is:


inline QString toPlainText() const
{ return document()->toPlainText(); }

For the second, str in foo() is not the return value of str in foo2().

See my last post in my thread where identify the problem with otext() at: http://www.qtcentre.org/threads/40572-Using-an-array-and-QString-contains

Nazgul
12th April 2011, 07:40
My Main ui class has the ui member, like this.


NPE::NPE(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}

void NPE::Update()
{
MessageBoxA(0,"he","called",0); // this works, it gets called
ui.Tabelwdiget->setrowcount(5);
}


My other class who wants to fill the listview


Dispatch::Dispatch
{
NPE xx
xx.Update()
}

EDIT: forgot to add things,

euhm but when Update is called it does not work, no rows at the listview. But when I make the call from NPE::ClickedBut6 // examples*/ it does work, it's kinda weird.

ComaWhite
12th April 2011, 07:47
My Main ui class has the ui member, like this.


NPE::NPE(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}

void NPE::Update()
{
MessageBoxA(0,"he","called",0); // this works, it gets called
ui.Tabelwdiget->setrowcount(5);
}


My other class who wants to fill the listview


Dispatch::Dispatch
{
NPE xx
xx.Update()
}

EDIT: forgot to add things,

euhm but when Update is called it does not work, no rows at the listview. But when I make the call from NPE::ClickedBut6 // examples*/ it does work, it's kinda weird.

Why are you using MessageBoxA instead of QMessageBox? Also you have to show us the update class. Another tip, you should use Qt's api style instead of your own because it mixes up naming conventions.

Nazgul
12th April 2011, 08:39
I only use MesageBox() when I debug etc.

Euhm Update() is a member of NPE shown in NPE class, with the messagebox inside it.

wysota
12th April 2011, 14:04
My Main ui class has the ui member, like this.


NPE::NPE(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}

void NPE::Update()
{
MessageBoxA(0,"he","called",0); // this works, it gets called
ui.Tabelwdiget->setrowcount(5);
}


My other class who wants to fill the listview


Dispatch::Dispatch
{
NPE xx
xx.Update()
}

EDIT: forgot to add things,

euhm but when Update is called it does not work, no rows at the listview. But when I make the call from NPE::ClickedBut6 // examples*/ it does work, it's kinda weird.

You have the exact same problem as the other guy - you are using a local variable that goes out of scope and is destroyed. Learn the difference between a class and an instance of a class (object).

Nazgul
12th April 2011, 22:44
O I see now, but what would my best option be to fix this,

Include Ui::NPEclass to my dispatch class and recreate a pointer to Ui?

ChrisW67
12th April 2011, 23:56
Your Dispatch class has to have a pointer (or reference) to the specific instance of your NPE class (i.e an object of class NPE) that your user is interacting with. It may be that you could pass a pointer to the NPE instance into the Dispatch constructor and store it in a member variable, or provide a method to supply the pointer after the Dispatch object is created. Then you have access to the NPE object through that pointer.