PDA

View Full Version : AnalogClock in mainwindow (in Label)



Lodhart
15th March 2009, 10:53
Hi,
there is my problem:
I heve a :
analog.h/.cpp with example analog code
main.h/.cpp main window
and main.h/.cpp

When i have:
AnalogClock *clock = new AnalogClock;
CalCen.setMainWidget( clock );
clock->show();
it's greate but the Clock is in another window.
So, how can i get the clock to for example to QLabel as QPixmap or smtls.

Thanks, Lodhart.

Lykurg
15th March 2009, 11:09
What's CalCen and why do you want to set the clock to a label. The clock itself is a showable widget. Please provide some more code to find your error.

aamer4yu
15th March 2009, 11:11
Why cant you show the clock as it is ? It seems to be inherited from QWidget, and you can simply use it as another widget in your application.

But say if you want to show that clock as pixmap in a label, you can do this -
1. Use a timer of say 200ms. In the timeout event call AnalogClock::render() [see QWidget::render]. Now you have rendered the clock on a pixmap, set this new pixmap onto yuor label.

2. You can inherit from QLabel and in the painevent of that label, do the rendering as above.

Lodhart
15th March 2009, 11:39
Its not Error. Clock is visible another window. Not IN main window. And i do not know how i can visible clock in MainWindow, say in frame.

---
CalCen is QApplication.

Lykurg
15th March 2009, 11:44
AnalogClock *clock = new AnalogClock;
CalCen.setMainWidget( clock );
clock->show();

Where do you have this code? (if it in main() I guess calling show on the clock will remove it from your mainwindow) please show us your constructor of CalCen. it should look like
CalCen::CalCen(...) : QMainwindow(...)
{
//...
AnalogClock *clock = new AnalogClock;
setMainWidget( clock );
}

Lodhart
15th March 2009, 11:53
int main(int argc, char *argv[])
{
QApplication CalCen(argc, argv); //application
MainWindow Main_form;

// inicialization //
Main_form.LoadIni();

AnalogClock *clock = new AnalogClock;
CalCen.setMainWidget( clock );
clock->show();

Main_form.show(); //otevreni hlavniho okna

return CalCen.exec();
//delete clock;
}

Lodhart
15th March 2009, 11:54
In analogclock.h

class AnalogClock : public QWidget
{
Q_OBJECT

public:
AnalogClock(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);
};

Lykurg
15th March 2009, 12:06
It must be

Main_form.setMainWidget( clock );

Lodhart
15th March 2009, 12:08
Yeah, I'll tray thet but:
error: 'class MainWindow' has no member named 'setMainWidget'
and i include Q3SUpport...

Lykurg
15th March 2009, 12:12
right, it's setCentralWidget() haven't read carefully. sorry.

faldzip
15th March 2009, 12:18
What I see is that you dont pass any parent to an analog clock. Pass the Main_form and it will show inside a Main_form. But your problem is that you are showing 2 different widgets (QMainWindow, AnalogClock) which are not connected at all. So they appear in separate windows. You have to add your clock to some layout in some other widget.
First you should delete this:


CalCen.setMainWidget( clock );
clock->show();

and then try this:


Main_form.setCentralWidget(clock);

but it may overwrite what's LoadIni() did.
So try:


Main_form.layout()->addWidget(clock);

Lodhart
15th March 2009, 12:24
Good ;), one another step to finish.. But now i have a big clock over the mainwindow and i can't see nothing else :D.

LoadIni is function in mainwindow.cpp to load pictures, set window and component geometry, ...

So what exactly i mean:

faldzip
15th March 2009, 13:20
Can you show the LoadIni() code?
You have to set up the clock position in MainWindow and it would be good to do it in LoadIni(). But it would be even better to do in MainWindow constructor the things which LoadIni() does.
The simplest way:


MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
{
// . . . setupUI or some members initialization
LoadIni();
}

Lodhart
15th March 2009, 21:49
I try change the position of Clock.. In main, in loadIni, in paintClock... but yes it works but all window is white color...

So I try import analogClock to class mainWindow. But it is the same problem, but clock is painting behin all component. I realy don't now.

faldzip
15th March 2009, 23:11
Can you show us the code? I think that all you need is layout in your centralWidget of MainWindow... are you using layouts? Please, provide some code explaining how you want to put clock in your MainWindow.

Lodhart
15th March 2009, 23:23
i don't know how i use layout...

I change the source code:

in mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);

//analogove hodiny
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
}


void MainWindow::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();

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);
}


}

in mainwindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindowClass *ui;

protected:
void paintEvent(QPaintEvent *event);
};

this is fajn but, the analogclock is painting on background of mainwindow. And i don't knowhow i can paint clock to widget, say the geometry is (10, 10, 200, 200) and transparent...
So, look like analogclock in QTDesigner, but there is a only example...

faldzip
15th March 2009, 23:38
Ok, I made small example. see it below. Is this something you want?

Lodhart
15th March 2009, 23:40
yeah, you are realy helpful ;-) so can you send me source file ?

faldzip
15th March 2009, 23:52
So you have to read some docs and tutorials about layouts. They are for positioning widgets inside other widgets. There are some types of layouts, like QGridLayout, QVBoxLayout, QHBoxLayout. I made some almost everything in my window in Qt Designer and it looks there like on the attached pic. Then I added layout to the groupbox "Clock" and then an AnalogClock to that layout.
Here is MainWindow constructor:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
m_clock = new AnalogClock(this);
ui->groupBox_2->setLayout(new QVBoxLayout);
ui->groupBox_2->layout()->addWidget(m_clock);

connect(ui->actionExit, SIGNAL(triggered()), SLOT(close()));
}

and the MainWindow class:


#include <QtGui/QMainWindow>
#include <QVBoxLayout>
#include "analogclock.h"

namespace Ui
{
class MainWindowClass;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindowClass *ui;
AnalogClock * m_clock;
};

faldzip
15th March 2009, 23:57
Here is the compressed project.

Lodhart
16th March 2009, 00:10
ouuuu, how simple ;-)... thank you very much, i'm new with Qt and Qt C++ and Delphi Pascal is much different then i expect. I know that i have to get some object to another object but i don't know how!.. There is a lot of to read... :(