Hello.
I am having trouble finding a way to update a widget. I'm modifying the fridge magnets example that comes with the Qt SDK.
Basically what I want to do is to be able to update the DragWidget contents by pressing a button. By update, I mean redrawing the draggable labels to reflect changes in the text file that it reads in order to draw the labels. For instance, I want to change a word in the text file, and after i click a button, i want the labels to be redrawn based on the changes in the text file.
In order to do that, what I did was create a new class that would contain both the DragWidget object and the button. I called it wrapwidget, and here's its definition and implementation:
{
Q_OBJECT
public:
wrapWidget();
};
wrapWidget::wrapWidget()
{
DragWidget *w = new DragWidget();
gridlayout->addWidget(w,0,0);
gridlayout->addWidget(b,1,0);
setLayout(gridlayout);
connect(b,SIGNAL(clicked()),w,SLOT(draw()));
}
class wrapWidget: public QWidget
{
Q_OBJECT
public:
wrapWidget();
};
wrapWidget::wrapWidget()
{
QGridLayout *gridlayout= new QGridLayout();
DragWidget *w = new DragWidget();
QPushButton *b = new QPushButton("refresh");
gridlayout->addWidget(w,0,0);
gridlayout->addWidget(b,1,0);
setLayout(gridlayout);
connect(b,SIGNAL(clicked()),w,SLOT(draw()));
}
To copy to clipboard, switch view to plain text mode
Here's the draw() slot definition:
void DragWidget::draw(){
QFile dictionaryFile
(":/dictionary/words.txt");
dictionaryFile.
open(QFile::ReadOnly);
int x = 5;
int y = 5;
while (!inputStream.atEnd()) {
inputStream >> word;
if (!word.isEmpty()) {
DragLabel *wordLabel = new DragLabel(word, this);
wordLabel->move(x, y);
wordLabel->show();
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
x += wordLabel->width() + 2;
if (x >= 245) {
x = 5;
y += wordLabel->height() + 2;
}
}
}
}
void DragWidget::draw(){
QFile dictionaryFile(":/dictionary/words.txt");
dictionaryFile.open(QFile::ReadOnly);
QTextStream inputStream(&dictionaryFile);
int x = 5;
int y = 5;
while (!inputStream.atEnd()) {
QString word;
inputStream >> word;
if (!word.isEmpty()) {
DragLabel *wordLabel = new DragLabel(word, this);
wordLabel->move(x, y);
wordLabel->show();
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
x += wordLabel->width() + 2;
if (x >= 245) {
x = 5;
y += wordLabel->height() + 2;
}
}
}
}
To copy to clipboard, switch view to plain text mode
That code was originally in the DragWidget constructor, I moved it out to the draw() method. I tried to set draw() as a slot, and call it by connecting it with the button's clicked() signal. It says there's no such slot though. What am I doing wrong?
Any tips on how I should be doing that?
Bookmarks