Results 1 to 13 of 13

Thread: Need help with QToolButton

  1. #1
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Need help with QToolButton

    Qt 3
    I have some window which have a dock window.This dock window creates a QToolBar
    mw->addToolBar(); mw it is a pointer to MainWindow.
    This function creates ToolBar.
    void MainWindow::addToolBar()
    {
    QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);

    }
    It works.
    When some event happens I must create button on toolbar.
    I do it so:
    mw->addButton();

    void MainWindow::addButton()
    {
    QToolBar* toolbar_but;
    btnpix = new QToolButton(toolbar_but, "");
    }
    One button it creates.
    But when next event happens -> Segmentation fault .
    What it could be?

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with QToolButton

    try

    Qt Code:
    1. void MainWindow::addButton()
    2. {
    3. QToolBar* tb = new QToolButton(DockLeft, "");
    4. }
    To copy to clipboard, switch view to plain text mode 

    BTW what is btnpix?
    Last edited by munna; 27th October 2006 at 13:56.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with QToolButton

    Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().

    Quote Originally Posted by philipp1 View Post
    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. // you should store toolbar_but as a member variable for later usage
    4. QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
    5. }
    6.  
    7. void MainWindow::addButton()
    8. {
    9. // the toolbar_but here is not the same toolbar_but as in addToolBar() !!
    10. QToolBar* toolbar_but; // this is just a local variable, a dangling pointer pointing nowhere
    11. btnpix = new QToolButton(toolbar_but, "");
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    philipp1 (27th October 2006)

  5. #4
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Need help with QToolButton

    May be I dont understand something but how we can ti convert QToolButton to QToolBar?
    For expierence I wrote your code: cannot convert 'QToolButton*' to 'QToolBar*' in initialization.

  6. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with QToolButton

    oh sorry that was my mistake.

    I actually meant

    Qt Code:
    1. void MainWindow::addButton()
    2. {
    3. QToolButton* tb = new QToolButton(DockLeft, "");
    4. }
    To copy to clipboard, switch view to plain text mode 

    Sorry once again.

  7. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with QToolButton

    Quote Originally Posted by jpn
    Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().

    Quote Originally Posted by philipp1
    One button it creates.
    It's strange.

  8. #7
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Need help with QToolButton

    Quote Originally Posted by munna View Post
    oh sorry that was my mistake.

    I actually meant

    Qt Code:
    1. void MainWindow::addButton()
    2. {
    3. QToolButton* tb = new QToolButton(DockLeft, "");
    4. }
    To copy to clipboard, switch view to plain text mode 

    Sorry once again.
    They is no such constructor QToolButton(DockLeft, ""); (I did't find it)

  9. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with QToolButton

    oh, I am sorry once again

    It should be

    Qt Code:
    1. void MainWindow::addButton()
    2. {
    3. QToolButton* tb = new QToolButton(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Actually I got confused because of the QToolBar consstructor that your are using.

    The constructor that you are using is obsolete

    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);// this is obsolete
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    you should rather be using

    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. QToolBar* toolbar_but = new QToolBar(this,0);
    4. toolbar_but->setLabel("Open Pictures");
    5. }
    To copy to clipboard, switch view to plain text mode 

    or may be the other one according to your need.

  10. #9
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Need help with QToolButton

    Quote Originally Posted by jpn View Post
    Store toolbar_but as a member variable. The toolbar_but in addButton() is different than in addToolBar(). It's just a dangling pointer in addButton().
    I understand you. But how can I realise it? Toolbutton belongs to mainwindow and I call him in a dock window.

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with QToolButton

    Sorry, I didn't understand. Realise what? You mean how can it be implemented?

    Qt Code:
    1. // class declaration
    2. class MainWindow : public ...
    3. {
    4. public:
    5. ...
    6.  
    7. private:
    8. QToolBar* toolbar_but; // <--- a member variable
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // class implementation
    2. void MainWindow::addToolBar()
    3. {
    4. // store the toolbar as a member variable for later usage
    5. toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
    6. }
    7.  
    8. void MainWindow::addButton()
    9. {
    10. // use the member variable here
    11. btnpix = new QToolButton(toolbar_but, "");
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    philipp1 (27th October 2006)

  13. #11
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Need help with QToolButton

    I made as you say from the wery beginning.


    class MainWindow : public QMainWindow {
    Q_OBJECT

    public:
    ....
    QToolBar* toolbar_but;
    void addToolBar();
    void addButton();

    mainwindow.cpp

    void MainWindow::addButton()
    {
    //QToolBar* toolbar_but;
    btnpix = new QToolButton(toolbar_but, "");
    }

    void MainWindow::addToolBar()
    {
    QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);

  14. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with QToolButton

    Quote Originally Posted by philipp1 View Post
    I made as you say from the wery beginning.

    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. // the problem is in here
    4. QToolBar* toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
    5. }
    To copy to clipboard, switch view to plain text mode 
    You are storing the toolbar to a local variable, not to the member variable. It should be:
    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. // notice the missing type, you want to use the member variable, not to create a new local variable
    4. toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    philipp1 (27th October 2006)

  16. #13
    Join Date
    Oct 2006
    Posts
    11
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Smile Re: Need help with QToolButton

    Quote Originally Posted by jpn View Post
    You are storing the toolbar to a local variable, not to the member variable. It should be:
    Qt Code:
    1. void MainWindow::addToolBar()
    2. {
    3. // notice the missing type, you want to use the member variable, not to create a new local variable
    4. toolbar_but = new QToolBar("Open puctures",this, DockLeft , TRUE ,0);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Have a great thanks from me. Puting types was my mistake.
    //(may be its friday)

Similar Threads

  1. Cannot hide QToolButton
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2006, 09:41
  2. Problem with qtoolbutton
    By moowy in forum Qt Programming
    Replies: 1
    Last Post: 22nd September 2006, 13:30
  3. Need help with QToolButton
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2006, 14:55

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.