PDA

View Full Version : update tab's title when contents change



alexei
26th September 2010, 19:59
Hello,

I'm building (or rather playing with) a Qt 4.7 app which basically consists of a window that contains a menu bar, a tab widget and a status bar. Each tab in the tab widgets contains a plain text editor. So basically, it's a tabbed plain text editor :)
Until now, I've managed to get it to create/open/save/close files, however, I haven't been able to change the tab text when the contents of the QPlainTextEdit change. It's simple when I open/save the file, but I don't know how to do it otherwise.
Any ideas?
In a SDI app I would normally do something like:

connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
but in this case, the text editor is no longer the central widget.
I've tried (in the newFile method):

EditorTab *editorTab = new EditorTab;
int tabIndex = tabWidget->addTab(
editorTab,
editorTab->currentTitle
);
tabWidget->setCurrentIndex(tabIndex);
connect(
editorTab->document(),
SIGNAL(contentsChanged()),
this,
SLOT(documentWasModified(tabIndex))
);
editorTab->setFocus();
documentWasModified is:

qDebug("Tab %d changed", tabIndex);
tabWidget->setTabText(tabIndex, "CHANGED");
And the result is

Object::connect: No such slot MainWindow::documentWasModified(tabIndex) in ..\tabbedtextedit\mainwindow.cpp:85:crying:

JohannesMunk
26th September 2010, 20:06
Well the QObject warning says it all. Your documentWasModified is not properly declared (as slot).

You can't connect slots which require a parameter to a signal that doesn't give one!

Make sure it looks something like this:


class MainWindow : ..
{ Q_OBJECT
public:
...
protected slots:
void documentWasModified();

};

void MainWindow::documentWasModified()
{
// get the tab index from your tabwidget currentIndex..
}
HIH

Johannes

alexei
26th September 2010, 20:39
Makes sense, thanks!

Ginsengelf
27th September 2010, 08:10
connect(editorTab->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified(tabIndex)));
Hi, this won't work because you cannot pass arguments in the connect call. QSignalMapper might be helpful.

Ginsengelf