(seemingly) simple Qt app: creating a progress bar
-
Yes, the contents of the main method would go into the worker class. Make the Modem and the flags a class variable, and the rest into the run slot. The singal and emit is just like in the dummy example we had before.
[EDIT: the contents of the main method goes to the class, not the method itself, of course :) ]
-
OK, thanks, Volker. On a related subject, I've been experimenting with creating a QMainWindow, but evidently, I don't understand the constructors as well as I thought. The line:
@ QProgressBar bar(0);@
Is creating an object of type QProgressBar named bar, and with no parent, right? So why doesn't this:
@ QMainWindow mainWindow;
QProgressBar bar(*mainWindow);
@Create a QMainWindow, and then a bar whose parent is the QMainWindow? I'm getting a "no matching function" compiler error.
-
You'd want @QProgessBar bar(&mainWindow)@
(&foo takes the address of an object, where *foo dereferences a pointer to an object)
But...
That's a bad idea! In this case, QProgressBar is created on the stack. Parenting it to QMainWindow would make QMainWindow attempt to take ownership of it (and, as such, try to delete it upon its own destruction, which would cause all kinds of badness to happen) You never want to call delete() on a stack-based item.
However, the following would be ok:
@
QMainWindow mainWindow;
QProgressBar *bar = new QProgressBar(&mainWindow);
@ -
mlong is correct here. In general, for real world applications, you should create QWidget based objects on the heap using new. If you set a parent, you do not need to call delete on those, that's handled by Qt automatically.
Also, you should consider using Qt Designer for creating the UI components. I personally find it much more convenient and easier to use than hard coding that stuff manually.
-
Thank you both for that. So, as a follow-up question...if it's taboo to create bar on the stack, why is it OK to create mainWindow on the stack? (And I realize this is main(), but I see the reasoning in general.)
And, Volker...I'll go look more closely at Qt Designer for awhile. I assume that whatever I come up with there can be neatly stitched back into my sample application?
EDIT:
I just tried the suggestion above:
@ QMainWindow mainWindow;
QProgressBar *bar = new QProgressBar(&mainWindow);
@But now the linker is complaining about my second connect() call, which I modified to look like this:
@ QObject::connect(&worker, SIGNAL(percentChanged(int)), *bar, SLOT(setValue(int)));
@(It's another "no matching function" error; I can provide the text if desired.) What did I do to cause this?
-
Ah, you're trapped by the pointer-reference-address-dereferencing confusion.
connect takes a pointer for the first and third argument. Which operator (* or & or nothing) you need to prefix the variable with depends on the type of the variable:
@
// stack based, w1 is an object
QWidget w1;// pointer to a QWidget
// & gets the address of w1
QWidget w1ptr = &w1;// reference to a QWidget
// no special operator is needed for the object w1
QWidget &w1ref1 = w1;// reference to a QWidget
// dereference the w1ptr with * to get the object
QWidget &w1ref2 = *w1ptr;// call a method:
w1.objectName();
w1ptr->objectName();
w1ref1.objectName();// ------------------------------
// heap based, w2 is a pointer
QWidget w2 = new QWidget(this);// pointer to a QWidget
// w2 is a pointer - no conversion needed
QWidget *w2ptr = w2;// reference to a QWidget
// dereference the w1ptr with * to get the object in both cases
QWidget &w2ref1 = *w2;
QWidget &w2ref2 = *w2ptr;// call a method:
w2->objectName();
w2ptr->objectName();
w2ref1.objectName();
w2ref2.objectName();
@So, back to connect:
In our first example, all object were created on the stack, and you had that objects in the variables. To make connect() happy, you need to make them to pointers using operator &.
In your modified code, you already have a pointer to the progress bar, so you do not need an operator at all:
@
QObject::connect(&worker, SIGNAL(percentChanged(int)), bar, SLOT(setValue(int)));
@Passing *bar would be the very same as passing bar (without operator) in the first version.
I suggest reading some C++ tutorial on that topic. I'm sure they explain this much better than me :-)
-
Hey, Volker...you explained that just fine. That was just a brain-fade on my part. So, back to my earlier question: if bar needs to be on the heap, why doesn't mainWindow?
Also, as long as I'm throwing out random questions: I notice my program no longer terminates. When I get to the end of file, can I signal a dismissal or something from the worker thread?
Alternatively, I can create a "quit" button or something; at that point, a little text and this thing is ready to go.
Thanks.
EDIT: one more random question: I was experimenting with adding text to the progress bar. (I know I can get it to show the precent completed, but right now, I'm being lazy and just going with "xxx." I'm still cracking the code of the documentation syntax, so I'm not sure how to form this: I tried:
@ QString text = "xxx";
bar->text = text;
@
But that didn't work (presumably because the text property is private). So I tried:
@ QString text = "xxx";
bar->text(text);
@
But that didn't work either. The compiler is giving me an error that I'm having trouble interpreting (something about using "const" in the call). What am I doing wrong here? -
Ah...after looking at the doc a bit more closely, I believe I misunderstood the text() function. It appears to be a getter, not a setter, so I can see how that wouldn't have worked. What's less clear is how I do set the text.
Here's my calls to the bar object:
@ bar->setRange(0, 100);
bar->setValue(0);
bar->move(10, 10);
bar->setTextVisible(true);
bar->show();
@What am I missing here to get the progress text to display? Thanks.
-
The "format property":/doc/qt-4.8/qprogressbar.html#format-prop of QProgressBar holds the text template that is displayed. Be aware that it's possible that the text is not displayed at all, e.g. in the Mac style!
For your quit problem:
By default, the application is not terminated once the last window has been closed. You can change this by adding the following line to your main method:@
QApplication app(argc, argv);app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
@A quit button does work too, of course.
-
I did look at that (honestly!)...I figured that since it said:
@The default value is "%p%".@
...that would mean it would show up by default. No big deal; it was more for my education anyway.
Thanks for the info on the quit slot. I do plan on adding a "close" button; just taking on one thing at a time. Right now, I'm fighting a weird Windoze build/debug problem.
More questions to follow soon...thanks.
EDIT:
Regarding the quit slot you suggested above, I now get a rather ominous message in the console window when I close the app window:
bq. QThread: Destroyed while thread is still running
Have I left some loose ends here? For example, am I supposed to write my own quit() to overload whatever the one supplied by QApplication does?
-
OK, I'm back to this. I'm ready to add a "close" button. In fact, I've already added the button object using QPushButton. Before I wade into connecting this up with signals and slots, I have a couple housekeeping questions:
- I still am not sure I understand why the completion percentage doesn't show up in text format. The way I read the documentation, by default, "%p%" will display, and just to make sure, I added a line:
@ bar->setFormat("%p%");
@But nothing shows up.
- I now have a QMainWindow objects that "contains" (if that's the right term) two other objects: QProgressBar and QPushButton. Each of these objects will need some formatting code, as will (I imagine) any other objects I might add. Currently, I have a routine to configure the progress bar:
@void configBar(QProgressBar* bar)
{
QString text;
bar->setRange(0, 100);
bar->setValue(0);
bar->move(50, 50);
bar->setTextVisible(true);
bar->setFormat("%p%");
bar->show();
text = bar->text();
}
@As you can see, I pass it a pointer to the object as a parameter. It wouldn't seem to make sense that I'd have a separate routine for each object; is there some way to derive the "contained" objects from QMainWindow? That way, I could do all my formatting in one routine. Or, should I create my own class, with all of these objects as member elements, and access them that way?
Thanks.
-
-
The setFormat as written above should set the text to "0%" through "100%" as the progress bar progresses. But, as Volker said, keep in mind that text may or may not be displayed (depending on the widget style in use.) The %p is the percentage, and the trailing % is just a percent sign. %m would be used for the current step, and %v would be used for the actual value.
-
The widgets in your mainwindow should be declared as private: member variables in the mainwindow header file. (This is basic encapsulation, so I hope I'm not saying anything too obvious to you.) Then you can have one setup routine. Traditionally (though it's a matter of style) I would something such as:
mainwindow.h:
@
class MainWindow : public QMainWindow
{
Q_OBJECT
...
void MainWindow(QWidget *parent = 0);
...
private:
void setupWidgets();QProgressBar *m_bar;
QPushButton *m_button;
// etc.
};
@and in the .cpp file:
@
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_bar = new QProgressBar(this); // these are children of MainWindow
m_button = new QPushButton(this);setupWidgets();
}
...
void MainWindow::setupWidgets()
{
// setup ProgressBar
m_bar->setRange(0, 100);
m_bar->setValue(0);
m_bar->move(50, 50);
m_bar->setTextVisible(true);
m_bar->setFormat("%p%");
// since calling MainWindow::show() will call show() on its children, there's no need to use m_bar->show().// setup PushButton
m_button->setText("Close");// can also set up any appropriate signals/slots, etc. here
}
@
(Brain to terminal. For illustrative purposes only.)I'm not sure why you're setting the "text" variable in your code above, but if you needed it somewhere else within the MainWindow class, it would be accessable via "m_bar->text()"
-
-
Thanks for the clarification on #2 above. I was only putting text in the button as a convenience; I'll probably do it as you suggest above.
I still don't get this:
bq. But, as Volker said, keep in mind that text may or may not be displayed (depending on the widget style in use.)
What is this widget style you're talking about? Do I have control over it?
Thanks.
-
Mlong -
I've begun re-coding based on your suggestion above. It presents an issue, though. If I create objects (like m_bar) as private members of another object (like mainWindow), then this line of code obviously doesn't work:
@ QObject::connect(&worker, SIGNAL(percentChanged(int)), bar, SLOT(setValue(int)));
@So, is the solution to define a slot within mainWindow, and somehow re-deliver the signal to m_bar? Or, does Qt offer a somewhat more automated way of accomplishing this?
Thanks.
-
bq. What is this widget style you’re talking about? Do I have control over it?
On a Mac, Qt draws widgets in a Mac style. On Windows, it uses Windows' style. And so on. There are a number of ways to override the default style (although not all styles are available on all platforms -- you can't have Mac-styled widgets on Windows, for instance.) See "here":http://developer.qt.nokia.com/doc/qt-4.8/style-reference.html for some more details.
bq. So, is the solution to define a slot within mainWindow, and somehow re-deliver the signal to m_bar?
That's probably the most straightforward way I can think of offhand. Just create a public slot in MainWindow that will pass the information on to m_bar. That way you just expose the functionality that the world outside your widget needs to have access to, rather than exposing the entire QProgressBar. It makes the interface to your MainWindow class a little more tidy, in that it can have an interface to the outside world that is directly-related to the business at hand.
-
Thanks for the clarification about styles. It makes sense, now...it explains why Qt applications don't look identical on all platforms.
I took a stab at changing the signal to go to the mainWindow object. It compiles, but doesn't work:
in main.cpp, the new connect looks like:
@ QObject::connect(&worker,
SIGNAL(percentChanged(int)),
&mainWindow,
SLOT(setBarValue(int)));
@and in mainwindow.cpp, I believe I need something like:
@void MainWindow::setBarValue(int i)
{
emit barValueChanged(i);
}
@But I'm not sure where to go from here. Do I need another connect() call, and/or do I implement another slot to handle this signal, or...not?
-
Oh, yes...because QProgressBar already has the smarts to know what to do with this, right? Got it. Thanks, Volker.
OK, I guess the next step is to empower the close button. (Then maybe a start/stop button, and that'll be that for this exercise.) I gather that I'm probably to use this signal:
@void QAbstractButton::clicked ( bool checked = false ) [signal]@
Assuming this is right:
- would the connect() call most appropriately go within the MainWindow constructor?
- I gather the first half of the connect() would look like this:
@ connect(&m_button, SIGNAL(clicked(bool)), )
@but what would be the object for the third parameter? And, what is the signal to QMainWindow to get it to close? I can't find anything in the doc about closing (except in the saveState() method, which I'm pretty sure isn't relevant to this).
Thanks for the help.
-
The quick and dirty option would be:
@
connect(m_button, SIGNAL(clicked()), qApp, SLOT(quit()));
@This just terminates the QApplication object, which in turn shuts down the rest of your application. Note that the bool parameter is not necessary here. It's sufficient to know that the button was clicked.
This is quite "rude", though, as it does not ask the user, if it was ok to quit the application and the worker thread doesn't get a chance to finish its work cleanly. So what you could do, is connect the signal to a slot in the main window class, ask the user if he/she really wants to quit the application (using one of the static [[Doc:QMessageBox]] methods). If yes, send the thread a signal to terminate its work, wait until the thread has finished (using wait, probably with a timeout). Some outline would be:
@
// move the thread and the worker out of the main method
// to the MainWindow class// so you have
QThread *m_thread;
Worker *m_worker;// define a signal
requestWorkerToFinish();// in constructor of MainWindow:
connect(m_button, SIGNAL(clicked()), this, SLOT(onQuitButtonClicked()));
connect(this, SIGNAL(requestWorkerToFinish()), m_worker, SLOT(requestFinish()));// you have to implement requestFinish in the worker!
void MainWindow::onQuitButtonClicked()
{
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Quit applicaton?"),
tr("Do you really want to quit the application?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No
);if(answer == QMessageBox::No) // user clicked no, so just continue as if nothing had happened return; emit requestWorkerToFinish(); // no direct call! bool finishOk = m_thread->wait(30000); if(!finishOk) { qDebug() << "WARNING: request to finish thread timed out!"; m_thread->terminate(); m_thread->wait(); } close();
}
@This is brain to terminal and untested. Please expect errors :-)
Just a hint: if you do like outlined here, don't forget to reimplement closeEvent() in the main window. Otherwise your users are able to just nuke off your application by clicking on the (X) button in the title bar of the window!
-
Hi, Volker -
Since I'm not exactly breezing through this stuff, I'd like to take it step by step, so I'll begin with the quick and dirty option.
- Just to keep my example program consistent with the code snippets in this thread, I tried renaming my QApplication from app to qApp. The build objected, claiming:
bq. error: no matching function for call to 'QApplication::QApplication(QApplication*)
I searched the docs for "qApp" and found that it's a global pointer, so evidently I shouldn't have tried to use it as a variable name. So, I reverted to the original variable name.
- from what context (function) should the above connect call be made? If I do it in the mainwindow c'tor, it doesn't "see" my QApplication object (which lives in main()).
I suspect that the answers to these two issues are related. Can you tell me what's going on here? Should my QApplication object be global or something?
I can post code if desired. Thanks.