Results 1 to 4 of 4

Thread: Add new TreeItemWidget using GUI of different class

  1. #1
    Join Date
    May 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Post Add new TreeItemWidget using GUI of different class

    Hello,
    I want to add QTreeWidgetItem using QPussbutton and QlineEdit of different class. here is my code

    mainWindow.h

    Qt Code:
    1. #include <QMainWindow>
    2. #include "name.h"
    3. .
    4. .
    5. .
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. void addNewItemToTree(QTreeWidgetItem *item);
    13.  
    14. private;
    15.  
    16. Ui::MainWindow *ui;
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    mainWindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. .
    3. .
    4. .
    5. void MainWindow::addNewItemToTree(QTreeWidgetItem *item)
    6. {
    7. ui->treeWidget->setColumncount(1);
    8. ui->treeWidget->addTopLevelItem(item);
    9. }
    To copy to clipboard, switch view to plain text mode 

    name.h
    Qt Code:
    1. #include <QWidget>
    2. #include "mainwindow.h"
    3. .
    4. .
    5. .
    6. class Name : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. Qstring getName(void);
    13.  
    14. private:
    15.  
    16. void on_Pussbutton_clicked();
    17.  
    18. UI::Name * ui;
    To copy to clipboard, switch view to plain text mode 

    name.cpp

    Qt Code:
    1. #include "name.h"
    2. .
    3. .
    4. .
    5. QString Name:: getName (void)
    6. {
    7. return ui->lineEdit->text();
    8. }
    9.  
    10. void Name::on_PussButton_clicked()
    11. {
    12.  
    13. item->setText(0,getName());
    14.  
    15.  
    16. MainWindow * mainW; // use poiter of MainWindow class
    17. mainW->addNewItemToTree(item); // add New Item in the fuction of MainWindow
    18. }
    To copy to clipboard, switch view to plain text mode 
    (Code hab been typed so plese ignor smalls mistacks)
    I am using these code. After clicking pussbutton item is not added in TreeWidget even I am not getting output either neither error.

    looking for your guidance or solution..

    Thanks..

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add new TreeItemWidget using GUI of different class

    Hi, I think you need to write
    Qt Code:
    1. private slots:
    To copy to clipboard, switch view to plain text mode 
    in line 14 of name.h so that the MOC can see which functions are slots.

    Ginsengelf

    edit: next time, please copy and paste the code, because something like
    Qt Code:
    1. MainWindow * mainW; // use poiter of MainWindow class
    2. mainW->addNewItemToTree(item); // add New Item in the fuction of MainWindow
    To copy to clipboard, switch view to plain text mode 
    will crash, and we don't know which errors are just "small mistakes" and which are actual code.

  3. #3
    Join Date
    May 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add new TreeItemWidget using GUI of different class

    Thank you for your reply..

    sorry, for mistakes and I have already written Private Slots: in name.h line number 14 still it doesnt work..

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add new TreeItemWidget using GUI of different class

    I have already written Private Slots: in name.h
    The correct syntax is

    Qt Code:
    1. private slots:
    To copy to clipboard, switch view to plain text mode 

    not

    Qt Code:
    1. Private Slots:
    To copy to clipboard, switch view to plain text mode 

    And like the previous poster said, if you want help solving code problems, you must post your actual code, not something that sort of looks like your code but really isn't. How are we supposed to know what is a coding mistake versus what is a typing mistake?

    Where is the connect() statement where you connect the QPushButton::clicked() signal to the on_PussButton_clicked() slot? If you did this in Qt Designer, is your button actually named "PussButton"?
    Last edited by d_stranz; 7th June 2016 at 22:12.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 13th January 2014, 09:24
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  3. Replies: 1
    Last Post: 20th January 2012, 04:39
  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.