PDA

View Full Version : append slots not... appending?



Slip Nine
26th May 2006, 09:22
I'm new to Qt and have some bare (console) c++ experience, so I expect this is some small error of mine, but for the life of me, I can't figure it out.

I can call myTextEdit->append() just fine in static code, but connecting a signal to it doesn't seem to work:



#include <QApplication>
#include <QObject>
#include <QWidget>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>

int main(int argc, char *argv[]) {

QApplication app(argc, argv);

QWidget* window = new QWidget;
QPushButton* aButton = new QPushButton("A Button");
QTextEdit* aTextEdit = new QTextEdit();
QTextDocument* aDocument = new QTextDocument("Text");

aButton->setFont(QFont("Impact", 18, QFont::Bold));
aButton->setGeometry(10, 40, 180, 40);
aTextEdit->setDocument(aDocument);
aTextEdit->append("\nSome more text");

QObject::connect(aButton, SIGNAL(clicked()), aTextEdit, SLOT(append("\nSome Dynamic Text")));

QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(aTextEdit);
layout->addWidget(aButton);

window->resize(300, 300);
window->setLayout(layout);
window->show();

return app.exec();
}


Other slots I connect to seem to work fine (Such as clear(), cut(), paste(), etc.) but append(), setPlainText(), etc... basically anything that would achieve what I want, doesn't do anything. I've tried these commands on the QDocument the QTextEdit contains as well-- both automatic and self-created. I've tried using QStrings and blocks of static test for the append() function (Both work fine in static code) and still no luck.

I hate to think that this could be a result of my relative newness to c++, but the other slots work, so I don't think that's it...

Appending QListWidgets seems to have the same problem, but I haven't done extensive testing.

vratojr
26th May 2006, 09:40
Hi,you program doesn't work because the signal and the slot you are using doesn't have the same signature:

The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)

Moreover, I think that you can't also pass the variable as the argument of the slot (eg: "\n this is the text") because you have to specify only the TYPE of the variable (QString in this case) that you have to pass. In this case you could do something like his:


connedt(abutton,SIGNAL(clicked()),textedit,SLOT(wr ite()));
void TextEdit::wrtite(){ append("This is the text");}