Purple Error in Widget Project
-
Looking at the output, you can see that the errors are being generated from ui_clock_application.h, though. Unless your Stopwatch class is defined in ui_clock_application.h, then you're looking in the wrong place.
As an aside, you have a memory leak in your timer_Start() slot. You are creating a new QTimer every time the slot is called. This is bad, because when you create a new one, the old one still exists.
In your Stopwatch constructor, you should have the line:
@
stopwatchTimer = 0; // or NULL
@And then in timer_Start():
@
void Stopwatch::timer_Start()
{
if (!stopwatchTimer) // Only do this stuff if it hasn't been done before.
{
stopwatchTimer = new QTimer();
connect(stopwatchTimer, SIGNAL(timeout()), this, SLOT(timer_changeTime()));
}
stopwatchTimer->start(1000);
}
@(Alternately, you could move the creation and connection of the QTimer into the constructor, and just call stopwatchTimer->start(...) from your slot.)
-
Yeah, the errors are generated in that file, but you cannot save changes to that file.
I will try and fix up that memory leak you mentioned. Thanks.
-
True, you can't save changes to that file, but those items are generated from your .ui file. Make sure that in the .ui file that any signal/slot connections you are making are pointing at the right objects.
-
The arrows are all pointing at the MainWindow (or Clock_Application), so this is what made me wonder whether you can only declare slots in the MainWindow (or Clock_Application) class.
-
There's nothing magical about MainWindow or Clock_Application or anything like that. There's just a connection being made somewhere (looks like in your .ui file) which is still expecting the timer_Start() slot (etc.) to be in the old location. Just moving the slot definition isn't enough to make the .ui file look point to the right place.
What are you doing in the clock_application.ui file which calls timer_Start()? What triggers timer_Start() to be called?
-
timer_Start() is triggered by a click of a button in the widget application. The button is a simple QPushButton.
-
Where is the connection made?
-
When you say "connection" do you refer to my connect() function? If so, here it is:
@
void Stopwatch::timer_Start()
{
stopwatchTimer = new QTimer();
connect(stopwatchTimer, SIGNAL(timeout()), this, SLOT(timer_changeTime()));
stopwatchTimer->start(1000);
}
@ -
No. What makes the button call timer_Start(). How do you tell it to do this?
-
I use that one menu in Qt Designer that allows you to draw blue/red arrows. I then create a new slot, and call it "timer_Start()", and I believe, this automatically connects the clicked() signal and the timer_Start() slot.
-
The arrows need to point to Stopwatch, not Clock_Application, if the slots are defined in Stopwatch.
Behind the scenes, those arrows translate to a "connect()" call in the ui_clock_application.h file.
-
How do I get it to "point" to Stopwatch? Stopwatch is just a class. It does not really refer to any widget.
-
Ah. Good point. In that case, I'd say that there shouldn't be any arrows anywhere, and that you should make the connection in code.
Edit to add:
Alternately, you could keep "proxy" slots in your Clock_Application widget, and from within those slots just call the slots in Stopwatch.
That is to say:
@
class Clock_Application ...
{
Q_OBJECT
...
public slots:
void timer_Start() { m_stopwatch->timer_Start(); } // just "pass through" to your Stopwatch class. (Remember that slots are just plain old methods too, and can be directly called like any other method.)
...
private:
Stopwatch *m_stopwatch;
};
@ -
Hmm.. now I am receiving a strange error:
multiple definition of
ui
first defined here
collect2: Id returned 1 exit statusThe lines in where the errors occur do not seem to be in my project, but in other files.. possibly library files? It's also strange, because I have defined my own
ui
just previously, and am wondering what is causing it now.