Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: QTabWidget with same tabs

  1. #1
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTabWidget with same tabs

    Hi! I am using qtabwidget to show some data, and I need to dinamicaly add tabs. Each tabs need to have same widgets (couple of qlinedits, qtablewidget, groupbox etc...) How can I add tabs with same objects on it, without drawing manually objects on every new tab?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTabWidget with same tabs

    Create a QWidget derived class with the lineedits and further GUI-Elements you need for the page and introduce a function to create an Object of this class whenever a new tab is requested and then insert/append this widget as a new page into the QTabWidget.

  3. #3
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    Thanx for reply, I did that, but I get a wierd behaviour from my application. I am using this code to generate my qtabwidget
    Qt Code:
    1. tabTCDescription=new QTabWidget(this);
    2. tabTCDescription->addTab(new Platform(),"Platform_0");
    3. //Platform is my class which inherits QWidget
    4. tabTCDescription->setGeometry(QRect(260, 230, 431, 311));
    5. //tabTCDescription is qtabwidget
    To copy to clipboard, switch view to plain text mode 
    When I run this code in constructor, tabwidget is shown. When I put it in my slot method (where I need it actually) it doesn't show tabwidget. What am I doing wrong?

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

    guilugi (9th January 2007)

  5. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget with same tabs

    Hi,

    In your slot function, you're running all these lines ?
    If so, you're creating a new TabWidget each time the slot is called..
    You have to create a single tabwidget for the whole class in your constructor, and only call addTab() in your slot!

  6. #5
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    Hi! I solved that, you are right. But there is a different workaround (btw, I am deleting tabwidget in the same slot, so I can create it). The workaround is to call show() method when the tabwidget is created. It works fine for me. I have different problem now. I have created class which inherits QWidget. Now, this class has those QLineEdits and other elements. When I create several tabs I can't really acces the appropriate tab to fill the right data. This is my code
    Qt Code:
    1. //this is how I add tab
    2. platformTab=new Platform(tabTCDescription);
    3. //Platform is my class which inherits QWidget, tabTCDescription is pointer on QTabWidget
    4. tabTCDescription->addTab(platformTab,"somestring");
    5.  
    6. //this is how I try to access appropriate Platform class to fill QLinedit:
    7. tempPlatformTab=(Platform*)tabTCDescription->widget(someinteger);
    8. //checked the someinteger, it is OK
    9. //and this is how I try to fill QLineEdit on my tab
    10. tempPlatformTab->editPlatformType->setText(tempString);
    11. //editPlatformType is QLineEdit
    To copy to clipboard, switch view to plain text mode 
    The result of code above is that all tabs are filled with same data. What I need to do, to access the appropriate tab?

  7. #6
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget with same tabs

    That's strange... you should check the last index, just after you inserted your tab!

    Something like widget( tabTCdescription->count()-1 ) should return you the last tab...

  8. #7
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    Thanks for advice but it doesn't work. My index is OK, tried it. I don't know what I am doing wrong. Here is my Platform class which inherits widget
    Qt Code:
    1. class Platform : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. QLabel * labelPlatformType;
    6. QLineEdit * editPlatformType;
    7. QLabel * labelActivatedStatus;
    8. QLineEdit * editActivatedStatus;
    9. QLabel * platformOS;
    10. QLineEdit * editPlatformOS;
    11. QLabel * timeout;
    12. QLineEdit * editTimeout;
    13.  
    14. Platform( QWidget *parent = 0);
    15. virtual ~Platform();
    16. };
    17.  
    18. //this is constructor
    19.  
    20. Platform::Platform(QWidget *parent): QWidget(parent)
    21. {
    22. labelPlatformType= new QLabel(tr("Platform Type :"));
    23. labelPlatformType->setParent(parent);
    24. labelPlatformType->setGeometry(QRect(20, 30, 80, 18));
    25. editPlatformType= new QLineEdit(parent);
    26. editPlatformType->setGeometry(QRect(100, 30, 113, 18));
    27. labelActivatedStatus= new QLabel(tr("Activated Status :"));
    28. labelActivatedStatus->setParent(parent);
    29. labelActivatedStatus->setGeometry(QRect(20, 50, 100, 18));
    30. editActivatedStatus= new QLineEdit(parent);
    31. editActivatedStatus->setGeometry(QRect(130, 50, 81, 18));
    32. platformOS= new QLabel(tr("Platform OS :"));
    33. platformOS->setParent(parent);
    34. platformOS->setGeometry(QRect(250, 30, 100, 18));
    35. editPlatformOS= new QLineEdit(parent);
    36. editPlatformOS->setGeometry(QRect(332, 30, 91, 18));
    37. timeout= new QLabel(tr("Timeout :"));
    38. timeout->setParent(parent);
    39. timeout->setGeometry(QRect(250, 50, 100, 18));
    40. editTimeout= new QLineEdit(parent);
    41. editTimeout->setGeometry(QRect(310, 50, 113, 18));
    42. }
    To copy to clipboard, switch view to plain text mode 
    Maybe you got some new ideas? I don't know whats wrong here

  9. #8
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget with same tabs

    Hard to tell just by seeing this code...it would be helpful if I had a compilable source code !

  10. #9
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    Well, i've pasted all code related to qtabwidget. I've noticed another thing now. When I run my application (i use Eclipse) i get this warning in console:
    QLayout::addChildWidget: Platform "" in wrong parent; moved to correct parent
    Maybe this gives hints to someone...

  11. #10
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget with same tabs

    Quote Originally Posted by Djony View Post
    Hi! I solved that, you are right. But there is a different workaround (btw, I am deleting tabwidget in the same slot, so I can create it).
    Can you show the code of this slot ? I guess you are doing something wrong in this slot. (Not sure , though).
    Also to which signal is this slot connected ?
    Last edited by Gopala Krishna; 10th January 2007 at 15:00. Reason: updated contents

  12. #11
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    No, I solved that. It's not about showing qtabwidget anymore. It's about writing the same data on all tabs (which is wrong, offcourse).

  13. #12
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget with same tabs

    Djony,

    If you can't, or if you don't want to send us your code, no problem, I understand !

    But, could you make just a small sample code with your tab classes, independent from your application, that we can compile and debug ?

    This would be faster to help, because I can't point the issue with your samples...

  14. #13
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget with same tabs

    Quote Originally Posted by Djony View Post
    When I create several tabs I can't really acces the appropriate tab to fill the right data. This is my code
    Qt Code:
    1. //this is how I try to access appropriate Platform class to fill QLinedit:
    2. tempPlatformTab=(Platform*)tabTCDescription->widget(someinteger);
    3. //checked the someinteger, it is OK
    4. //and this is how I try to fill QLineEdit on my tab
    5. tempPlatformTab->editPlatformType->setText(tempString);
    6. //editPlatformType is QLineEdit
    To copy to clipboard, switch view to plain text mode 
    The result of code above is that all tabs are filled with same data. What I need to do, to access the appropriate tab?

    Ok, where are you calling these functions ? In the same slot ?? If so it would help if you show your slot code and show to which signal the slot is connected to.

  15. #14
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    Actually, I solved it, it is very simple. If you look at code of constructor of Platform class, you will see that I use setParent(parent) with my edit controls. Well, I changed it all to setParent(this) and it works. Before that I tried not to set parent and it doesn;t work. So, solution is simple, it has nothing to do with my slot code Thanks for your effort guys!

  16. #15
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabWidget with same tabs

    Oh yes, I didn't even noticed that !

    I'm not even sure you need to use those setParent() calls...

  17. #16
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTabWidget with same tabs

    Yeah, instead of
    labelPlatformType= new QLabel(tr("Platform Type :"));
    labelPlatformType->setParent(this);
    You can just do

    labelPlatformType= new QLabel(tr("Platform Type :"), this);
    Software Engineer



  18. #17
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTabWidget with same tabs

    If that is really the entire constructor code of your Platform class, that means you are not using Layouts which in turn means you are doing something fundemantally wrong. If that is really the case you should introduce some layout within the Platform class and set the main layout of the Platform class to this. I didn't get to follow the thread yesterday, but as I started to read the posts today I noticed the wierd parent-child constructs. For the platform you have introduced, a QGridLayout is sufficient and appropriate.
    You don't need to declare Parents for the widgets since they are automatically reparented when adding them to the layout. So the correct way to do this, is to instantiate the desired Widgets and then to create a Layout and make it the main Layout of the Platform object.

    QGridLayout * lo = new QGridLayout;
    setLayout(lo);

    lo->addWidget(label, ....);
    Layouts are a very very important Part of Qt's Gui Programming. Generally, you have at least one Layout in your class, if it has more than one widget to be laid out ...


    cheers
    momesana
    Last edited by momesana; 11th January 2007 at 16:42.

  19. #18
    Join Date
    Dec 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QTabWidget with same tabs

    Quote Originally Posted by Djony View Post
    Hi! I solved that, you are right. But there is a different workaround (btw, I am deleting tabwidget in the same slot, so I can create it). The workaround is to call show() method when the tabwidget is created. It works fine for me. I have different problem now. I have created class which inherits QWidget. Now, this class has those QLineEdits and other elements. When I create several tabs I can't really acces the appropriate tab to fill the right data. This is my code
    Qt Code:
    1. //this is how I add tab
    2. platformTab=new Platform(tabTCDescription);
    3. //Platform is my class which inherits QWidget, tabTCDescription is pointer on QTabWidget
    4. tabTCDescription->addTab(platformTab,"somestring");
    5.  
    6. //this is how I try to access appropriate Platform class to fill QLinedit:
    7. tempPlatformTab=(Platform*)tabTCDescription->widget(someinteger);
    8. //checked the someinteger, it is OK
    9. //and this is how I try to fill QLineEdit on my tab
    10. tempPlatformTab->editPlatformType->setText(tempString);
    11. //editPlatformType is QLineEdit
    To copy to clipboard, switch view to plain text mode 
    The result of code above is that all tabs are filled with same data. What I need to do, to access the appropriate tab?



    Hi, How did you solved that? I'm facing the similar problem, getting segmentation fault (using windows Qt-4.7.4) while trying to insert or add same object in different tabs.

    code:
    static int tabIndex;
    PageInsideTab *ptr; // PageInsideTab is inherited from QWidget
    ptr = pageList.at(tabIndex); // QList <PageInsideTab *> pageList - I declared 15 pointers and stored into this list
    ptr = new PageInsideTab(tabWidget);
    qDebug()<<"tabIndex="<<tabIndex;
    tabWidget->insertTab(tabIndex, ptr, name); // name is QString
    // tabWidget->addTab(ptr, name);
    tabIndex++;


    when this slot is running first time one tab is inserted, all ok, but in the second run I'm getting seg. fault.
    Any solution please?

  20. #19
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget with same tabs

    because you are assigning the new page to a COPY of the pointer in the list.

    I strongly advise that you not populate your list with invalid pointers. Instead, append to your list when you have a valid pointer to add.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  21. #20
    Join Date
    Dec 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QTabWidget with same tabs

    Hi,
    As It's confidential, I'm only giving the relevant code.

    I created a form PageInsideTab.ui which is inherited from QWidget and I put a button and a textEdit on it.
    Now the header file:
    #ifndef CHATWINDOW_H
    #define CHATWINDOW_H

    #include <QtGui>
    #include <QWidget>
    #include <QTabWidget>
    #include "PageInsideTab.h"

    namespace Ui {
    class ChatWindow;
    }

    class ChatWindow : public QWidget
    {
    Q_OBJECT

    public:
    explicit ChatWindow(QWidget *parent = 0);
    ~ChatWindow();
    void closeEvent();
    QVBoxLayout *mainLayout;
    QTabWidget *tabWidget;
    QLabel *lbl_Message;
    QLineEdit *lineEdit_TextInput;
    QList <PageInsideTab *> pageList;


    private:
    Ui::ChatWindow *ui;

    public slots:
    void init();
    void returnPressed();
    void appendMessage(const QString &from, const QString &message);
    void setBuddyName(QString);
    void createNewTab(QString buddyName);
    void historyButtonClicked();
    void createPageList();
    };

    #endif // CHATWINDOW_H


    and part of the cpp file:


    void ChatWindow :: init()
    {
    mainLayout = new QVBoxLayout();
    setLayout(mainLayout);
    tabWidget = new QTabWidget();
    tabWidget->setMinimumSize(200,150);
    lbl_Message = new QLabel();
    lbl_Message->setText("Message:");
    lineEdit_TextInput = new QLineEdit();
    mainLayout->addWidget(tabWidget);
    mainLayout->addWidget(lbl_Message);
    mainLayout->addWidget(lineEdit_TextInput);
    tabWidget->show();
    lbl_Message->show();
    lineEdit_TextInput->show();
    }

    void ChatWindow :: createPageList()
    {
    PageInsideTab *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10, *p11, *p12, *p13, *p14, *p15;
    pageList.append(p1);
    pageList.append(p2);
    pageList.append(p3);
    pageList.append(p4);
    pageList.append(p5);
    pageList.append(p6);
    pageList.append(p7);
    pageList.append(p8);
    pageList.append(p9);
    pageList.append(p10);
    pageList.append(p11);
    pageList.append(p12);
    pageList.append(p13);
    pageList.append(p14);
    pageList.append(p15);
    }

    void ChatWindow :: createNewTab(QString buddyName)
    {
    static int tabIndex;
    PageInsideTab *ptr;
    // ptr = pageList.at(tabIndex);
    ptr = new PageInsideTab(tabWidget);
    //qDebug()<<"tabIndex="<<tabIndex;
    tabWidget->insertTab(tabIndex, ptr, buddyName);
    // tabWidget->addTab(ptr, buddyName);
    tabIndex++;
    }


    init() and createPageList() i'm calling in the constructor of this class(ChatWindow), and creatNewTab(QString) is
    called from another class supplying the parameter buddyName. This time I've commented the use of pageList pointers
    but in this case also, I'm getting SEGMENTATION fault in the SECOND run of the slot creatNewTab(QString).

Similar Threads

  1. Replies: 2
    Last Post: 23rd July 2012, 09:42
  2. QTabWidget - problem with resizing
    By moowy in forum Qt Programming
    Replies: 5
    Last Post: 14th September 2006, 15:06
  3. Switching off all tabs in QTabWidget
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 30th August 2006, 18:10
  4. Removing Tabs
    By NewGuy in forum Qt Programming
    Replies: 6
    Last Post: 22nd July 2006, 23:46
  5. Corner widget in QTabWidget doesn't show up
    By ePharaoh in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2006, 18:02

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.