Seite 1 von 1

[GELÖSt] Während Laufzeit dynamisch Tabs zum TabWidget hinzu

Verfasst: 26. März 2009 18:08
von madcat2709
Hallo an alle!

Bin was Qt und die Programmierei angeht noch recht neu und hoffe ihr könnt mir erklären warum bei meinem Quelltext nicht das passiert, was ich davon erwarte ;-)

Wie der Titel schon verrät würde ich den User gerne durch einfaches Klicken auf einen Menüeintrag ein weiteres Tab öffnen/erstellen lassen. Soweit klappt auch alles ganz gut und die auf dem Eintrag liegende Funktion (heißen bei Qt ja QAction ist auch mit dem richtigen Slot connected). Dann sollen von der Slotfunktion addPlotTab() QWidgets erstellt werden, die dann via Ui_NewTab.setupUi() ein vorgefertigtes Layout und Child-Widgets (hier hab ich den Qt Designer benutzt) zugeteilt bekommen. Anschließen werden die neuen Widgets als Tabs via addTab() hinzugefügt.

Das Problem ist nun das sich diese Tabs aber leider nicht zeigen. Das Tabwidget behält beim Debuggen während immer nur den einen Standarttab mit dem es auch erzeugt wurde und die während der Laufzeit durch den User hinzugefügten Tags zeigen sich nicht.

Im Folgenden der code der mit Kopfschmerzen bereitet ;-)

mainwindowimpl.h

Code: Alles auswählen

#ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H
//
#include <QMainWindow>
#include "ui_mainwindow.h"
//
class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
	MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
private slots:
	void addPlotTab();
};
#endif
mainwindowimpl.cpp

Code: Alles auswählen

#include "mainwindowimpl.h"
#include "ui_plottab.h"
#include <QtGui>
//
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
	setupUi(this);

	connect(actionNeues_PlotTab, SIGNAL(activated()), this, SLOT(addPlotTab()));
}
//
void MainWindowImpl::addPlotTab()
{
	QWidget NewTab;
	QString name="NewTab";
	Ui_PlotTab Ui_NewTab;
	Ui_NewTab.setupUi(&NewTab);
	//NewTab.show();
	//tabWidget->setUpdatesEnabled(true);
	tabWidget->addTab(&NewTab, name);
	tabWidget->show();
	//NewTab.show();
	update();
	int a = tabWidget->indexOf(&NewTab);
	QWidget NewTab2;
	QString name2="NewTab2";
	Ui_NewTab.setupUi(&NewTab2);
	tabWidget->addTab(&NewTab2, name2);
	int b = tabWidget->indexOf(&NewTab2);
	qDebug() << "I'm still not working!" << a << "\t" << b;
	QPushButton *test;
	test = new QPushButton(OldTab);
	test->setObjectName(QString::fromUtf8("test"));
	test->setGeometry(QRect(350, 20, 80, 27));
};
und das Ui das im neuen Tab erscheinen soll (Qt Designer-Code):

Code: Alles auswählen

class Ui_PlotTab
{
public:
    QGridLayout *gridLayout;
    QVBoxLayout *verticalLayout_3;
    QVBoxLayout *verticalLayout_2;
    QHBoxLayout *horizontalLayout_2;
    QLabel *label;
    QLineEdit *lineEdit;
    QHBoxLayout *horizontalLayout;
    QGroupBox *groupBox;
    QVBoxLayout *verticalLayout;
    QCheckBox *checkBox;
    QCheckBox *checkBox_2;
    QPushButton *pushButton;

    void setupUi(QWidget *PlotTab)
    {
    if (PlotTab->objectName().isEmpty())
        PlotTab->setObjectName(QString::fromUtf8("PlotTab"));
    PlotTab->resize(300, 164);
    gridLayout = new QGridLayout(PlotTab);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    verticalLayout_3 = new QVBoxLayout();
    verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
    verticalLayout_2 = new QVBoxLayout();
    verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
    horizontalLayout_2 = new QHBoxLayout();
    horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    label = new QLabel(PlotTab);
    label->setObjectName(QString::fromUtf8("label"));

    horizontalLayout_2->addWidget(label);

    lineEdit = new QLineEdit(PlotTab);
    lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

    horizontalLayout_2->addWidget(lineEdit);


    verticalLayout_2->addLayout(horizontalLayout_2);

    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    groupBox = new QGroupBox(PlotTab);
    groupBox->setObjectName(QString::fromUtf8("groupBox"));

    horizontalLayout->addWidget(groupBox);

    verticalLayout = new QVBoxLayout();
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    checkBox = new QCheckBox(PlotTab);
    checkBox->setObjectName(QString::fromUtf8("checkBox"));

    verticalLayout->addWidget(checkBox);

    checkBox_2 = new QCheckBox(PlotTab);
    checkBox_2->setObjectName(QString::fromUtf8("checkBox_2"));

    verticalLayout->addWidget(checkBox_2);


    horizontalLayout->addLayout(verticalLayout);


    verticalLayout_2->addLayout(horizontalLayout);


    verticalLayout_3->addLayout(verticalLayout_2);

    pushButton = new QPushButton(PlotTab);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));

    verticalLayout_3->addWidget(pushButton);


    gridLayout->addLayout(verticalLayout_3, 0, 0, 1, 1);


    retranslateUi(PlotTab);

    QMetaObject::connectSlotsByName(PlotTab);
    } // setupUi

    void retranslateUi(QWidget *PlotTab)
    {
    PlotTab->setWindowTitle(QApplication::translate("PlotTab", "Form", 0, QApplication::UnicodeUTF8));
    label->setText(QApplication::translate("PlotTab", "Function", 0, QApplication::UnicodeUTF8));
    groupBox->setTitle(QApplication::translate("PlotTab", "Settings", 0, QApplication::UnicodeUTF8));
    checkBox->setText(QApplication::translate("PlotTab", "Logscale", 0, QApplication::UnicodeUTF8));
    checkBox_2->setText(QApplication::translate("PlotTab", "LogLogscale", 0, QApplication::UnicodeUTF8));
    pushButton->setText(QApplication::translate("PlotTab", "Plot", 0, QApplication::UnicodeUTF8));
    Q_UNUSED(PlotTab);
    } // retranslateUi

};
Vielen Dank für eure Aufmerksamkeit!

Verfasst: 26. März 2009 18:18
von franzf
Hi,

Gib mal in deinem Slot addPlotTab() eine debug-message aus

Code: Alles auswählen

qDebug() << "Wanna add Plot";
oder so.
Du wirst sehen, dass der Slot nie aufegrufen wird. Starte dazu deine App in der Console. Da sollte auch noch eine andere Meldung wegen connect() kommen.

Problem ist: QAction kennt keine Slot namens "activated()". Was du willst ist "triggered()".

Also in MainWindowImpl den connect so ändern:

Code: Alles auswählen

connect(actionNeues_PlotTab, SIGNAL(triggered()), this, SLOT(addPlotTab()));
http://doc.trolltech.com/4.5/qaction.html#triggered

Verfasst: 26. März 2009 18:34
von madcat2709
Hey Danke für die schnelle Antwort!
Hab das Problem gefunden und deine Annahme scheint leider falsch zu sein, denn:

wenn du oben noch einmal nachchaust wirst du in der Slot-Funktion "addPlotTab()" die Zeile

Code: Alles auswählen

qDebug() << "I'm still not working!" << a << "\t" << b; 
finden die ich auch immer angezeigt bekommen hab, was letztlich heißt, dass auch das auch der connect inkl. Signal "activated()" funktioniert.

Nun aber zu Lösung: Die Instanzen der betreffenden Variablen sind einfach auf dem Stack generiert und werden nach Ende des Scopes also nach ausführen der Slot-Funktion "addPlotTab()" wieder zerstört. Damit kann ich natürlich auch kein neues Tab erscheinen sehen... Also alles auf dem Heap erzeugt und schon läufts.

Vielen Dank nochmal das ihr Zeit in mein Problem investiert habt ;-)