Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: AnalogClock in mainwindow (in Label)

  1. #1
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default AnalogClock in mainwindow (in Label)

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: AnalogClock in mainwindow (in Label)

    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.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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.

  4. #4
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: AnalogClock in mainwindow (in Label)

    Quote Originally Posted by Lodhart View Post
    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
    Qt Code:
    1. CalCen::CalCen(...) : QMainwindow(...)
    2. {
    3. //...
    4. AnalogClock *clock = new AnalogClock;
    5. setMainWidget( clock );
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication CalCen(argc, argv); //application
    4. MainWindow Main_form;
    5.  
    6. // inicialization //
    7. Main_form.LoadIni();
    8.  
    9. AnalogClock *clock = new AnalogClock;
    10. CalCen.setMainWidget( clock );
    11. clock->show();
    12.  
    13. Main_form.show(); //otevreni hlavniho okna
    14.  
    15. return CalCen.exec();
    16. //delete clock;
    17. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    In analogclock.h
    Qt Code:
    1. class AnalogClock : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AnalogClock(QWidget *parent = 0);
    7.  
    8. protected:
    9. void paintEvent(QPaintEvent *event);
    10. };
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: AnalogClock in mainwindow (in Label)

    It must be
    Qt Code:
    1. Main_form.setMainWidget( clock );
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    Yeah, I'll tray thet but:
    error: 'class MainWindow' has no member named 'setMainWidget'
    and i include Q3SUpport...

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: AnalogClock in mainwindow (in Label)

    right, it's setCentralWidget() haven't read carefully. sorry.

  11. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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:
    Qt Code:
    1. CalCen.setMainWidget( clock );
    2. clock->show();
    To copy to clipboard, switch view to plain text mode 
    and then try this:
    Qt Code:
    1. Main_form.setCentralWidget(clock);
    To copy to clipboard, switch view to plain text mode 
    but it may overwrite what's LoadIni() did.
    So try:
    Qt Code:
    1. Main_form.layout()->addWidget(clock);
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. #12
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    Good , one another step to finish.. But now i have a big clock over the mainwindow and i can't see nothing else .

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

    So what exactly i mean:
    Attached Images Attached Images
    Last edited by Lodhart; 15th March 2009 at 11:48.

  13. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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:
    Qt Code:
    1. MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
    2. {
    3. // . . . setupUI or some members initialization
    4. LoadIni();
    5. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  14. #14
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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.

  15. #15
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  16. #16
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    i don't know how i use layout...

    I change the source code:

    in mainwindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5.  
    6. //analogove hodiny
    7. QTimer *timer = new QTimer(this);
    8. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    9. timer->start(1000);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *)
    2. {
    3. static const QPoint hourHand[3] = {
    4. QPoint(7, 8),
    5. QPoint(-7, 8),
    6. QPoint(0, -40)
    7. };
    8. static const QPoint minuteHand[3] = {
    9. QPoint(7, 8),
    10. QPoint(-7, 8),
    11. QPoint(0, -70)
    12. };
    13.  
    14. QColor hourColor(127, 0, 127);
    15. QColor minuteColor(0, 127, 127, 191);
    16.  
    17. int side = qMin(width(), height());
    18. QTime time = QTime::currentTime();
    19.  
    20. QPainter painter(this);
    21.  
    22. painter.setRenderHint(QPainter::Antialiasing);
    23. painter.translate(width() / 2, height() / 2);
    24. painter.scale(side / 200.0, side / 200.0);
    25.  
    26. painter.setPen(Qt::NoPen);
    27. painter.setBrush(hourColor);
    28.  
    29. painter.save();
    30. painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
    31. painter.drawConvexPolygon(hourHand, 3);
    32. painter.restore();
    33.  
    34. painter.setPen(hourColor);
    35.  
    36. for (int i = 0; i < 12; ++i) {
    37. painter.drawLine(88, 0, 96, 0);
    38. painter.rotate(30.0);
    39. }
    40.  
    41. painter.setPen(Qt::NoPen);
    42. painter.setBrush(minuteColor);
    43.  
    44. painter.save();
    45. painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
    46. painter.drawConvexPolygon(minuteHand, 3);
    47. painter.restore();
    48.  
    49. painter.setPen(minuteColor);
    50.  
    51. for (int j = 0; j < 60; ++j) {
    52. if ((j % 5) != 0)
    53. painter.drawLine(92, 0, 96, 0);
    54. painter.rotate(6.0);
    55. }
    56.  
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

    in mainwindow.h
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private:
    10. Ui::MainWindowClass *ui;
    11.  
    12. protected:
    13. void paintEvent(QPaintEvent *event);
    14. };
    To copy to clipboard, switch view to plain text mode 

    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...

  17. #17
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    Ok, I made small example. see it below. Is this something you want?
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  18. #18
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: AnalogClock in mainwindow (in Label)

    yeah, you are realy helpful ;-) so can you send me source file ?
    Last edited by Lodhart; 15th March 2009 at 22:50.

  19. #19
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    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:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. m_clock = new AnalogClock(this);
    6. ui->groupBox_2->setLayout(new QVBoxLayout);
    7. ui->groupBox_2->layout()->addWidget(m_clock);
    8.  
    9. connect(ui->actionExit, SIGNAL(triggered()), SLOT(close()));
    10. }
    To copy to clipboard, switch view to plain text mode 
    and the MainWindow class:
    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include <QVBoxLayout>
    3. #include "analogclock.h"
    4.  
    5. namespace Ui
    6. {
    7. class MainWindowClass;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindowClass *ui;
    20. AnalogClock * m_clock;
    21. };
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by faldzip; 15th March 2009 at 22:53. Reason: Now with picture attached :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  20. #20
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: AnalogClock in mainwindow (in Label)

    Here is the compressed project.
    Attached Files Attached Files
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  21. The following user says thank you to faldzip for this useful post:

    Lodhart (15th March 2009)

Similar Threads

  1. mainwindow modality
    By user in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 00:54
  2. Notifying Mainwindow of an event..
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2007, 21:29
  3. Replies: 1
    Last Post: 11th September 2007, 13:34
  4. Replies: 1
    Last Post: 24th October 2006, 16:40
  5. Replies: 3
    Last Post: 23rd July 2006, 18:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.