PDA

View Full Version : Qt custom slot does not work



GG2013
4th June 2013, 05:37
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:


public slots:
void plotData();
void paintEvent(QPaintEvent *e);


In traffic.cpp


void Traffic::plotData()
{
.......
}




void Traffic::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter;
QFont font;
painter.begin(this);
Nightcharts PieChart;
PieChart.setType(Nightcharts::Dpie);//{Histogramm,Pie,DPie};
PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
PieChart.setCords(100,100,this->width()/1.5,this->height()/1.5);
PieChart.addPiece("Item1",QColor(200,10,50),34);
PieChart.addPiece("Item2",Qt::green,27);
PieChart.addPiece("Item3",Qt::cyan,14);
PieChart.addPiece("Item4",Qt::yellow,7);
PieChart.addPiece("Item5",Qt::blue,4);
PieChart.draw(&painter);
PieChart.drawLegend(&painter);
}

wysota
4th June 2013, 06:57
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.

anda_skoa
4th June 2013, 10:36
The slot you are looking for is called update() and exists in all widgets

Cheers,
_

GG2013
5th June 2013, 00:20
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.


void Traffic::paint()
{
drawChart = true; //in paintEvent() i use a check for this variable
update();
}

ChrisW67
5th June 2013, 02:39
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.

GG2013
5th June 2013, 06:00
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.

GG2013
7th June 2013, 05:15
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.

9115

wysota
7th June 2013, 06:58
You are painting on the wrong widget.

GG2013
7th June 2013, 07:03
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.

wysota
7th June 2013, 07:12
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.

GG2013
10th June 2013, 07:58
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:


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

[code]
void MainWindow::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter;
QFont font;
painter.begin(this);
Nightcharts PieChart;
PieChart.setType(Nightcharts::Dpie);//{Histogramm,Pie,DPie};
PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
PieChart.setCords(100,100,this->width()/1.5,this->height()/1.5);
PieChart.addPiece("Item1",QColor(200,10,50),34);
PieChart.addPiece("Item2",Qt::green,27);
PieChart.addPiece("Item3",Qt::cyan,14);
PieChart.addPiece("Item4",Qt::yellow,7);
PieChart.addPiece("Item5",Qt::blue,4);
PieChart.draw(&painter);
PieChart.drawLegend(&painter);
}

anda_skoa
10th June 2013, 16:07
Why on earth are you overriding MainWindow::paintEvent()?

Cheers,
_

GG2013
12th June 2013, 04:27
Finally I got it right...Thank you everybody who helped me to resolve this.