Results 1 to 10 of 10

Thread: Add Item in other Class

  1. #1
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Add Item in other Class

    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

  2. #2
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add Item in other Class

    For the ui class, are you talking about something along these lines:

    Qt Code:
    1. foo()
    2. {
    3. Ui::MainWindow mw;
    4. QString str = mw.textEdit->toPlaintText();
    5. }
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. foo()
    2. {
    3. MainWindow mw;
    4. QString str = mw.foo2();
    5. }
    6.  
    7. QString MainWindow::foo2()
    8. {
    9. QString str = ui.textEdit->toPlaintText();
    10. return str
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add Item in other Class

    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:


    Quote Originally Posted by TomJoad View Post
    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?
    Last edited by ChrisW67; 12th April 2011 at 00:21.

  4. #4
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add Item in other Class

    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:

    Qt Code:
    1. inline QString toPlainText() const
    2. { return document()->toPlainText(); }
    To copy to clipboard, switch view to plain text mode 

    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/4057...tring-contains

  5. #5
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add Item in other Class

    My Main ui class has the ui member, like this.

    Qt Code:
    1. NPE::NPE(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. }
    6.  
    7. void NPE::Update()
    8. {
    9. MessageBoxA(0,"he","called",0); // this works, it gets called
    10. ui.Tabelwdiget->setrowcount(5);
    11. }
    To copy to clipboard, switch view to plain text mode 

    My other class who wants to fill the listview

    Qt Code:
    1. Dispatch::Dispatch
    2. {
    3. NPE xx
    4. xx.Update()
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Add Item in other Class

    Quote Originally Posted by Nazgul View Post
    My Main ui class has the ui member, like this.

    Qt Code:
    1. NPE::NPE(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. }
    6.  
    7. void NPE::Update()
    8. {
    9. MessageBoxA(0,"he","called",0); // this works, it gets called
    10. ui.Tabelwdiget->setrowcount(5);
    11. }
    To copy to clipboard, switch view to plain text mode 

    My other class who wants to fill the listview

    Qt Code:
    1. Dispatch::Dispatch
    2. {
    3. NPE xx
    4. xx.Update()
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  7. #7
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add Item in other Class

    I only use MesageBox() when I debug etc.

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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add Item in other Class

    Quote Originally Posted by Nazgul View Post
    My Main ui class has the ui member, like this.

    Qt Code:
    1. NPE::NPE(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. }
    6.  
    7. void NPE::Update()
    8. {
    9. MessageBoxA(0,"he","called",0); // this works, it gets called
    10. ui.Tabelwdiget->setrowcount(5);
    11. }
    To copy to clipboard, switch view to plain text mode 

    My other class who wants to fill the listview

    Qt Code:
    1. Dispatch::Dispatch
    2. {
    3. NPE xx
    4. xx.Update()
    5. }
    To copy to clipboard, switch view to plain text mode 

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add Item in other Class

    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?

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add Item in other Class

    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.

Similar Threads

  1. Replies: 1
    Last Post: 8th April 2011, 19:54
  2. QGraphics Item custom class not compiling
    By budda in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2010, 08:56
  3. QList class member and item operations
    By whites11 in forum Newbie
    Replies: 2
    Last Post: 25th March 2010, 18:05
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.