Cannot create QtWidget
-
Hello Folks;
p. I'm new to Qt Development and am having a problem creating a custom class based on QtWidget. I've based my classes on the "elastic nodes" example code provided by Qt. I'm hoping someone might be able to tell me what I'm doing wrong. I'm using Qt 5.0.2.
Here is my aim:
Start my main application
Main application receives a request, via tcp, to open a new window
Signal is received by main application, and the appropriate slot is invoked
In the slot function, create a new custom Widget class and display it.
So far, everything works great until step 4, where I receive an exception and my application crashes.
Slot definition:
@
void MainWindow::SlotReadyRead() {
// TODO: Here is where you fail Ugh - the line below fails. Don't know why.
QVBoxLayout* lay = new QVBoxLayout;
_graphWidget = new GraphWidget(this); <== Fail spectacularly here
lay->addWidget(_graphWidget); <== What I hope will happen
@GraphWidget definition:
@
class GraphWidget : public QGraphicsView
{
Q_OBJECTpublic:
GraphWidget(QWidget *parent = 0);
~GraphWidget();
}
@GraphWidget constructor:
@
GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent), <== Failure occurs in QGraphicsView() constructor
_timerId(0)
{
init();
}
@p. And finally, the exception message follows.
Thank you very much for your time.
S.T.________ Exception Trace: ________
Process: TestApp [17075]
Path: /Users/USER/*/TestApp.app/Contents/MacOS/TestApp
Identifier: com.yourcompany.TestApp
Version: ???
Code Type: X86-64 (Native)
Parent Process: Qt Creator [5478]
User ID: 501Date/Time: 2013-08-04 14:10:46.378 -0400
OS Version: Mac OS X 10.8.4 (12E55)
Report Version: 10Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000Application Specific Information:
abort() calledThread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff84171212 __pthread_kill + 10
1 libsystem_c.dylib 0x00007fff82b0fb54 pthread_kill + 90
2 libsystem_c.dylib 0x00007fff82b53dce abort + 143
3 QtCore 0x0000000101304b09 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9
4 QtCore 0x0000000101305111 QMessageLogger::fatal(char const*, ...) const + 161
5 QtWidgets 0x0000000101ee577a QWidgetPrivate::QWidgetPrivate(int) + 618
6 QtWidgets 0x0000000101fd2aa6 QFramePrivate::QFramePrivate() + 22
7 QtWidgets 0x000000010204bcc1 QAbstractScrollAreaPrivate::QAbstractScrollAreaPrivate() + 17
8 QtWidgets 0x000000010219b404 QGraphicsViewPrivate::QGraphicsViewPrivate() + 20
9 QtWidgets 0x000000010219f8d5 QGraphicsView::QGraphicsView(QWidget*) + 37
10 GraphGUI 0x000000010089b1fd GraphWidget::GraphWidget(QWidget*) + 49 (GraphWidget.cpp:4)
11 com.yourcompany.TestApp 0x000000010000f6a5 MainWindow::SlotReadyRead() + 12493 (mainwindow.cpp:341)
12 com.yourcompany.TestApp 0x000000010005040f MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 111 (moc_mainwindow.cpp:241)
13 QtCore 0x000000010042b0c1 QMetaObject::activate(QObject*, int, int, void**) + 2593
14 QtNetwork 0x000000010013f454 QNetworkReplyHttpImplPrivate::replyDownloadProgressSlot(long long, long long) + 180
15 QtNetwork 0x00000001001b6725 QNetworkReplyHttpImpl::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 1061
16 QtCore 0x0000000100424438 QObject::event(QEvent*) + 808
17 QtWidgets 0x00000001008e4ca7 QApplicationPrivate::notify_helper(QObject*, QEvent*) + 311
18 QtWidgets 0x00000001008e610a QApplication::notify(QObject*, QEvent*) + 794
19 QtCore 0x00000001003fdd4e QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 766
20 QtGui 0x0000000100eed7f1 QWindowSystemInterface::sendWindowSystemEvents(QFlagsQEventLoop::ProcessEventsFlag) + 17
21 libqcocoa.dylib 0x000000010341bcc5 QCocoaEventDispatcherPrivate::processPostedEvents() + 149
22 libqcocoa.dylib 0x000000010341c5cb QCocoaEventDispatcherPrivate::postedEventsSourceCallback(void*) + 43
23 com.apple.CoreFoundation 0x00007fff8bdf1b31 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
24 com.apple.CoreFoundation 0x00007fff8bdf1455 __CFRunLoopDoSources0 + 245
25 com.apple.CoreFoundation 0x00007fff8be147f5 __CFRunLoopRun + 789
26 com.apple.CoreFoundation 0x00007fff8be140e2 CFRunLoopRunSpecific + 290
27 com.apple.HIToolbox 0x00007fff896c2eb4 RunCurrentEventLoopInMode + 209
28 com.apple.HIToolbox 0x00007fff896c2c52 ReceiveNextEventCommon + 356
29 com.apple.HIToolbox 0x00007fff896c2ae3 BlockUntilNextEventMatchingListInMode + 62
30 com.apple.AppKit 0x00007fff86c72533 _DPSNextEvent + 685
31 com.apple.AppKit 0x00007fff86c71df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
32 com.apple.AppKit 0x00007fff86c691a3 -[NSApplication run] + 517
33 libqcocoa.dylib 0x000000010341b2cb QCocoaEventDispatcher::processEvents(QFlagsQEventLoop::ProcessEventsFlag) + 1307
34 QtCore 0x00000001003fa983 QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) + 371
35 QtCore 0x00000001003fd705 QCoreApplication::exec() + 357
36 com.yourcompany.TestApp 0x0000000100042ec7 main + 13351 (main.cpp:414)
37 com.yourcompany.TestApp 0x0000000100005e54 start + 52 -
Edit: Removed original post. Answered my own question.
What is the message that Qt prints to the program's output when it fails?
(Line 4 in the back trace.) -
Hello ChirsW67;
Thank you very much for your response. The message emitted to the output window is:
QWidget: Must construct a QApplication before a QPaintDevice
The program has unexpectedly finished.
/Users/SquimbellyToes/Development/TestApp/build-TestApp-QT_5_0_2-Debug/TestApp/dist/TestApp.app/Contents/MacOS/TestApp exited with code 0Thank you,
S.T. -
[quote author="SquimbellyToes" date="1375746923"]QWidget: Must construct a QApplication before a QPaintDevice[/quote]Check your main() function -- have you created a QApplication object? (Not a QCoreApplication or a QGuiApplication)
-
Hello JKSH;
Thank you for chiming in!
Indeed I have:
@int main(int argc, char *argv[])
{
QApplication app(argc, argv);
... lots of initialization code...
return app.exec();
}@Thank you,
S.T. -
Do you have GUI objects declared as static global variables? That is, do you have things like :
@
static MainWindow w;
@
in global scope?BTW: It seems very strange to be modifying the layout of a UI in response to (potentially) every byte that is received.
-
Hello ChrisW67;
Thank you again for your attention. I very much appreciate your time and effort.
I do not have any static variables defined. As I am new to Qt programming, I'm a little confused by where I should put such a variable, especially since where my program crashes is within MainWindow.
I'm also feeling like I might not have provided enough context with my question. If I may, I'd like to clarify what is happening, in short, in my program:
-
My main program starts, and in main.cpp.main(int argc, char *argv[]) MainWindow is created:
@MainWindow w;
... some initialization of w
w.show();@ -
After MainWindow w.show(), I start a TCP server. MyTcpServer inherits from QDialog as it emits a number of signals which are connected to slots in MainWindow
@MyTcpServer tcpServer;
... init tcpServer...
tcpServer.Run();@- Then just before we leave main.cpp.main(...) I start the QApplication object "app":
@app.exec();@
And we leave main.cpp.main(...);
-
My MainWindow constructor is shamelessly stolen from the Qt documentation:
@MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}@ -
A client application, currently run from the command line, issues a call to MyTcpServer. Here's an abbreviation of the client application main(int argc, char *argv[]).
Again, the code is pretty much the same as what we find in the Qt documentation:
@QApplication a(argc, argv);
MyTcpClient tcpClient;
... initialization ...
QString tcpClientMsg = tcpClient.SendRequest(qsOperation, qslParams);
// the Tools:: namespace is my own library of helper functions
if(!Tools::IsEqual(tcpClientMsg, "OK"))
{
iReturnValue = -3;
tcpClient.DisplayErrorMessage(tcpClientMsg);
tcpClient.CloseClientPublic();
}
a.exec();@ -
MyTcpServer receives the MyTcpClient request, processes it and emits a signal which MainWindow receives, via it's appropriate slot. The MainWindow slot performs a remote database call via a QNetworkRequest().
-
Still in MainWindow the QNetworkReply object, returned by the QNetworkRequest, has it's QNetworkRequest::readyRead() Signal connected to the MainWindow::SlotReadyRead() slot.
-
When the QNetworkReply::readyRead() signal is emitted, the MainWindow::SlotReadyRead() slot is called and the program crashes:
@void MainWindow::SlotReadyRead() {
// TODO: Here is where you fail Ugh - the line below fails. Don't know why.
QVBoxLayout* lay = new QVBoxLayout;
_graphWidget = new GraphWidget(this); <== Fail spectacularly here
lay->addWidget(_graphWidget); <== What I hope will happen@
Boy, that explanation was longer than I had hoped it would be.
Again, thank you for your time and assistance!
S.T. -
-
If you did have static variables like the one I asked about then they are constructed before main() runs and therefore before QApplication is constructed. This may lead to errors of the type you describe. That would be an unusual thing to do though.
Do you use threading in MyTcpServer?
Do you have both the client and the server in the same executable? They talk to each other?
What is in the constructor of GraphWidget? -
Hello ChrisW67;
I'm very lucky to have you persevere with me - thank you very much.
The answers to to your queries follows.
-
There are no global variables in my program as you describe.
-
Do you use threading in MyTcpServer?
No, I am not using threading in MyTcpServer. -
Do you have both the client and the server in the same executable?
The server is instantiated and executed by my main program. The client application is a standalone command line program and is executed on demand and independently of my main program. -
They talk to each other?
The server knows nothing of the client. The communication is from client to server only. -
What is in the constructor of GraphWidget?
As follows:
@GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent), _timerId(0)
{
DEBUG_METHOD();
init();
}GraphWidget::~GraphWidget()
{
DEBUG_METHOD();
}@Thank you very much ChrisW67,
S.T. -