PDA

View Full Version : Qt creates a main.o file he cannot execute



sylas
1st December 2016, 06:36
Hi all. I found the World Time Clock Example | Qt 4.8 and tried to create it with my Qt Creator. Documentation gives many files but no main.cpp available. Seems strange !
I created the main.cpp missing (see code below). The project compiles well, but, at the execution, (which is available) he says "no executable specified". Does it mean "cannot execute this main.o" ?? Here is the code of my main.cpp
#include <QApplication>
#include "worldtimeclock.h"
#include "worldtimeclockplugin.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
WorldTimeClock clock;
#if defined(Q_OS_SYMBIAN)
clock.showMaximized();
#else
clock.show();
#endif
return app.exec();
}


Sorry, I don't find how to tag the above code !

d_stranz
1st December 2016, 19:01
"main.o" is not an executable program. It is a binary object file resulting from compilation of your main.cpp, and needs to be linked with other object files and run-time libraries to create the executable.

You have apparently configured your Qt project incorrectly in Qt Creator or the .pro file. Without more information it is impossible to tell what you did wrong. I am very surprised that the world time example did not have a file containing a "main()" function (The file does not have to be called "main.cpp") or a .pro file that is configured correctly.

Edit: Are you trying to build the World Time Clock Plugin example (http://doc.qt.io/qt-4.8/qt-designer-worldtimeclockplugin-example.html)? This project does not build an executable program, it builds a Qt Designer plugin library. You cannot add a main() function to it and create a runnable executable.

sylas
2nd December 2016, 06:24
The documentation gives :
Files:

designer/worldtimeclockplugin/worldtimeclock.cpp
designer/worldtimeclockplugin/worldtimeclock.h
designer/worldtimeclockplugin/worldtimeclockplugin.cpp
designer/worldtimeclockplugin/worldtimeclockplugin.h
designer/worldtimeclockplugin/worldtimeclockplugin.pro
I made the error to put all of them in my project. It created many .o files , the first being main.o. Maybe if I suppress some of the above files I will be able to see the famous clock !!

d_stranz
2nd December 2016, 16:46
Maybe if I suppress some of the above files I will be able to see the famous clock !!

No. The project is configured to build a Qt Designer plugin.

If you copy the worldtimeclock.h and worldtimeclock.cpp files (and nothing else from the plugin project) into a new project that builds an executable GUI app, edit worldtimeclock.h to remove the "QT_DESIGNER_EXPORT" macro from the class definition, and create an instance of the WorldTimeClock widget as your apps, window, then you will be able to see it.

sylas
3rd December 2016, 08:37
I am so curious to see that clock, that I will try what you say. It is a big job indeed. Thank you very much.

d_stranz
3rd December 2016, 22:25
It isn't a big job at all:



// main.cpp

#include <QApplication>
#include "WorldTimeClock.h"

int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
WorldTimeClock c;
c.show();
return app.exec();
}


Add main.cpp, worldtimeclock.cpp, and worldtimeclock.h to a Qt project, compile it, and run.

sylas
4th December 2016, 05:14
I removed the "QT_DESIGNER_EXPORT" as you told me but many errors appear. I also pasted your new main.I am unable to go further. Here is the code (no # available):
//mainwindow.h


#ifndef WORLDTIMECLOCK_H
#define WORLDTIMECLOCK_H

#include <QTime>
#include <QWidget>
#include <QtUiPlugin/QDesignerExportWidget>//error:No such file or directory
class QDESIGNER_WIDGET_EXPORT WorldTimeClock : public QWidget
{
Q_OBJECT

public:
explicit WorldTimeClock(QWidget *parent = 0);

public slots:
void setTimeZone(int hourOffset);

signals:
void updated(QTime currentTime);

protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
int timeZoneOffset;
};

#endif

anda_skoa
4th December 2016, 08:11
I removed the "QT_DESIGNER_EXPORT" as you told me but many errors appear.

Obviously also remove the include that would be needed for the designer plugin that you are no longer building.
Also remove the QDESIGNER_WIDGET_EXPORT, as its name suggests it is also needed for the designer plugin, which you are no longer using.



Here is the code (no # available)


Yes, there is. Did you switch to Advanced mode?
In any case you can also type the tags:
and the code in-between

Cheers,
_

sylas
4th December 2016, 10:41
I made what you told me. No clock appears (run available). Compile output : Elapsed time: 00:03 Application Output: exited code 0 Issues: too many remarks. My O.S. is Linux 16. Strange enough, I have another project of another clock named "Analog Clock" ; It shows very well. I give you my last code of the header:


//worldtimeclock.h
#ifndef WORLDTIMECLOCK_H
#define WORLDTIMECLOCK_H
#include <QTime>
#include <QWidget>
//#include <QtUiPlugin/QDesignerExportWidget>//error:No such file or directory
class //QDESIGNER_WIDGET_EXPORT WorldTimeClock : public QWidget
{
Q_OBJECT

public:
explicit WorldTimeClock(QWidget *parent = 0);

public slots:
void setTimeZone(int hourOffset);

signals:
void updated(QTime currentTime);

protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
int timeZoneOffset;
};

#endif

sylas
4th December 2016, 13:45
Excuse me. Compiler finds errors in main.cpp(you gave me). aotnd the header file(modified on your demand). I am tired and I am not experieced enough with Qt Creator.

d_stranz
5th December 2016, 01:13
class //QDESIGNER_WIDGET_EXPORT WorldTimeClock : public QWidget

You are obviously not very experienced with C++ either. This code comments out everything after the word "class" on this line, which obviously won't compile. When we say "remove QDESIGNER_WIDGET_EXPORT" we mean exactly that: delete the letters "Q T D E S I G N E R _ W I D G E T _ E X P O R T", then save the file.

I wrote a program for you in a previous post in this thread. If you follow our instructions instead of trying to do your own thing, that program would work. It is almost exactly the same as the AnalogClock example, except you substitute a "WorldTimeClock" widget for the "AnalogClock" widget.

sylas
5th December 2016, 12:25
Only one error but a jumping error. First in the header, line 22. Compiler doesn't know Q_DECL_OVERRIDE. Where is it ? It is i the file worldtimeclockplugin. I add this new header. At last the error is QtUiPlugin/QDesignerExportWidget. Please tell me, did you see this clock ? I doubt i will ever see this clock.

d_stranz
5th December 2016, 17:29
You need three source files in your project (and only these three):

1 - main.cpp (which I have given you)
2 - worldtimeclock.h (which comes from the plugin project)
3 - worldtimeclock.cpp (which comes from the plugin project)

and no other source code files. Edit worldtimeclock.h to delete this line:

#include <QtUiPlugin/QDesignerExportWidget>

and to delete QDESIGNER_WIDGET_EXPORT. Delete Q_DECL_OVERRIDE if the compiler complains about it.

Save the file. Build the project. Run it. See your clock.

sylas
5th December 2016, 18:44
Still only one error. This time in "WorldTimeClock.cpp" . Error in line 15. Lokk at the comment please. Here is the code:

//WorldTimeClock.cpp
#include "WorldTimeClock.h"
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>

WorldTimeClock::WorldTimeClock(QWidget *parent)
: QWidget(parent)
, timeZoneOffset(0)

{
typedef void (QWidget::*WidgetUpdateSlot)();

QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, static_cast<WidgetUpdateSlot>(&QWidget::update));
//above line:no matching function for call to "WorldTimeClock::connect(QTimer*&,void(QTimer::*)(),WorldTimeClock*,void(QWidget::* )()
// ::*)()'
timer->start(1000);

setWindowTitle(tr("World Time Clock"));
resize(200, 200);
}

void WorldTimeClock::paintEvent(QPaintEvent *)
{
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -70)
};

QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);

int side = qMin(width(), height());
QTime time = QTime::currentTime();
time = time.addSecs(timeZoneOffset);

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);

painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);

painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();

painter.setPen(hourColor);

for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}

painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);

painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();

painter.setPen(minuteColor);

for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}

emit updated(time);
}

void WorldTimeClock::setTimeZone(int hourOffset)
{
timeZoneOffset = qMin(qMax(-12, hourOffset), 12) * 3600;
update();
}

d_stranz
6th December 2016, 01:22
Remove line 12. Change line 15 to:



connect(timer, SIGNAL(timeout()), this, SLOT(update()));

sylas
6th December 2016, 07:42
Hurrah ! I can see the clock. Thank you very much. Another day I will look for the way to display the legal time of LosAngeles.

anda_skoa
6th December 2016, 08:02
Line 15 should have also worked without the static_cast.


connect(timer, &QTimer::timeout, this, &QWidget::update);


Cheers,
_

sylas
6th December 2016, 13:23
I am always looking for help.I should like to change my local time. I thought if was in line: , timeZoneOffset(0) that is line 9. I changed the 0 in the parenthesis, but nothing happens. I don't see where else I should act. Thanks for your help.

d_stranz
6th December 2016, 17:27
Line 15 should have also worked without the static_cast.

Yes. I didn't see the point of the typedef or static_cast in the first place. I posted the old style connect() because I wasn't sure if the OP was using Qt4 or Qt5.



I changed the 0 in the parenthesis, but nothing happens.

time = time.addSecs(timeZoneOffset);

This statement implies that the time zone offset is expressed in seconds. The setTimeZone() method converts hours to seconds appropriately, so call that with the offset in hours instead of changing the value in the WorldTimeClock constructor:



// main.cpp

#include <QApplication>
#include "WorldTimeClock.h"

int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
WorldTimeClock c;
c.setTimeZone( -8 ); // Los Angeles - PST
c.show();
return app.exec();
}

sylas
6th December 2016, 19:40
Magnific ! You are a lion !

d_stranz
6th December 2016, 20:59
Want a second hand on your clock? Replace WorldTimeClock:: paintEvent() with this:



void WorldTimeClock::paintEvent(QPaintEvent *)
{
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};

static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -70)
};

static const QPoint secondHand[2] = {
QPoint( 0, 0 ),
QPoint( 0, -70 )
};

QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);

int side = qMin(width(), height());
QTime time = QTime::currentTime();
time = time.addSecs(timeZoneOffset);

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);

painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);

painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();

painter.setPen(hourColor);

for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}

painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);

painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();

painter.setPen(minuteColor);

painter.save();
painter.rotate( 6.0 * time.second() );
painter.drawLines( secondHand, 1 );
painter.restore();

for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}

emit updated(time);
}