Results 1 to 13 of 13

Thread: Qt custom slot does not work

  1. #1
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qt custom slot does not work

    Hallo,
    I have two entries (say, entry1 and entry2) in one menu. What I would like is to execute plotData() when entry1 is selected, and to execute paintEvent(QPaintEvent *e) when entry2 is selecetd. I am using Qt creator (ver 2.7.0) Signal and Slot editor to connect slots to signal. I have done it successfully with entry1 by using triggered() and plotData() (it appears automatically when I right-click slot) for signal and slot respectively. However, I can not do the same with the other function- it does not appear on slot list (I am using triggered() as signal).

    Any help would be greatly appreciated.
    In traffic.h:
    Qt Code:
    1. public slots:
    2. void plotData();
    3. void paintEvent(QPaintEvent *e);
    To copy to clipboard, switch view to plain text mode 

    In traffic.cpp
    Qt Code:
    1. void Traffic::plotData()
    2. {
    3. .......
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Traffic::paintEvent(QPaintEvent *e)
    2. {
    3. QWidget::paintEvent(e);
    4. QPainter painter;
    5. QFont font;
    6. painter.begin(this);
    7. Nightcharts PieChart;
    8. PieChart.setType(Nightcharts::Dpie);//{Histogramm,Pie,DPie};
    9. PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
    10. PieChart.setCords(100,100,this->width()/1.5,this->height()/1.5);
    11. PieChart.addPiece("Item1",QColor(200,10,50),34);
    12. PieChart.addPiece("Item2",Qt::green,27);
    13. PieChart.addPiece("Item3",Qt::cyan,14);
    14. PieChart.addPiece("Item4",Qt::yellow,7);
    15. PieChart.addPiece("Item5",Qt::blue,4);
    16. PieChart.draw(&painter);
    17. PieChart.drawLegend(&painter);
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt custom slot does not work

    Quote Originally Posted by GG2013 View Post
    What I would like is to execute plotData() when entry1 is selected, and to execute paintEvent(QPaintEvent *e) when entry2 is selecetd.
    This does not make any sense. paintEvent() is an event executed to update contents of the widget. In particular, it is not a slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt custom slot does not work

    The slot you are looking for is called update() and exists in all widgets

    Cheers,
    _

  4. #4
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Quote Originally Posted by anda_skoa View Post
    The slot you are looking for is called update() and exists in all widgets

    Cheers,
    _
    Thanks a lot...after a bit of search I can now use update() in a slot to activate paintEvent() as I wanted.
    Just one more question....in plotData() I use setCentralWidget() so that the graph is diaplayed in the main window. Now when I activate entry2 the pie chart
    is displayed in the background. How do I clear the window before update() is called? Or, how do I set the paintEvent() to happen in the foreground? So, basically what I want is on clicking entry1 button the graph is shown on main window, then on clicking entry2 the graph should diappear and pie chart will appear. Hope this makes sense.
    Qt Code:
    1. void Traffic::paint()
    2. {
    3. drawChart = true; //in paintEvent() i use a check for this variable
    4. update();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by GG2013; 5th June 2013 at 01:40. Reason: updated contents

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt custom slot does not work

    Put the graph widget and table widget into a QStackedWidget, which is the central widget of the QMainWindow, and have your menu actions switch between the two pages in the stack.

  6. #6
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Quote Originally Posted by ChrisW67 View Post
    Put the graph widget and table widget into a QStackedWidget, which is the central widget of the QMainWindow, and have your menu actions switch between the two pages in the stack.
    Thank you for your reply. Do you mind giving me at bit more details (I am really new to Qt/Qwt/NightCharts). If you look at my code earlier I have used plotData() (a data plot with Qwt) and paint() (a pie chart with NightCharts) as two slots which are to be activated with two different menu-clicks.
    Thank you for your time.

  7. #7
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Hallo All,
    I still could not figure out the above. In brief, I have 2 menu actions, for ex. Total Traffic & Network Layer- please see attached figure. The first one will trigger plotData() that draws a plot (Bandwidth Graph), and the second one will trigger a pie chart. The problem is that the pie chart is always shown behind the plot.

    As suggested by @ChrisW67 I tried to implement but still no luck.

    screen.jpg

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt custom slot does not work

    You are painting on the wrong widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Quote Originally Posted by wysota View Post
    You are painting on the wrong widget.
    Thank you for your reply. Could you please write me a bit details...I don't get it.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt custom slot does not work

    Your screenshot looks like you have a widget in your main window which is some kind of chart and at the same time you are trying to paint another chart directly in the main window widget itself. Since the first chart is a child widget of the main window, its contents will always be above the contents of the main window. Note that I'm basing my thoughts only on the screenshot as you didn't share any meaningful code with us.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Quote Originally Posted by wysota View Post
    Your screenshot looks like you have a widget in your main window which is some kind of chart and at the same time you are trying to paint another chart directly in the main window widget itself. Since the first chart is a child widget of the main window, its contents will always be above the contents of the main window. Note that I'm basing my thoughts only on the screenshot as you didn't share any meaningful code with us.
    Thank you for your reply. You are right- both are basically drawn on main window. But I still can't figure out how to access the widget for NightCharts (it's a class) (please see the code below) so that it can be added in QStackedWidget.


    In mainwindow.cpp:
    [code]
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ........
    stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(widget1);//here goes Qwt plot widget
    stackedWidget->addWidget(widget2); //here goes pie chart widget
    ......
    }

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *e)
    2. {
    3. QWidget::paintEvent(e);
    4. QPainter painter;
    5. QFont font;
    6. painter.begin(this);
    7. Nightcharts PieChart;
    8. PieChart.setType(Nightcharts::Dpie);//{Histogramm,Pie,DPie};
    9. PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
    10. PieChart.setCords(100,100,this->width()/1.5,this->height()/1.5);
    11. PieChart.addPiece("Item1",QColor(200,10,50),34);
    12. PieChart.addPiece("Item2",Qt::green,27);
    13. PieChart.addPiece("Item3",Qt::cyan,14);
    14. PieChart.addPiece("Item4",Qt::yellow,7);
    15. PieChart.addPiece("Item5",Qt::blue,4);
    16. PieChart.draw(&painter);
    17. PieChart.drawLegend(&painter);
    18. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt custom slot does not work

    Why on earth are you overriding MainWindow:aintEvent()?

    Cheers,
    _

  13. #13
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt custom slot does not work

    Finally I got it right...Thank you everybody who helped me to resolve this.

Similar Threads

  1. Slot doesn't work in new window
    By mohjlir in forum Newbie
    Replies: 2
    Last Post: 27th January 2011, 10:50
  2. slot no work in another class
    By Aronax in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2011, 10:21
  3. Replies: 5
    Last Post: 14th September 2010, 21:13
  4. Slot doesn't seem to work
    By waynew in forum Newbie
    Replies: 3
    Last Post: 10th November 2009, 09:54
  5. copy() slot doesn't work
    By Macok in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2009, 10:55

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.