Results 1 to 6 of 6

Thread: QDialog show method's wierd problem on signal slot mechanism.

  1. #1
    Join Date
    Jan 2010
    Location
    Ankara - TURKEY
    Posts
    30
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QDialog show method's wierd problem on signal slot mechanism.

    Hi All,

    I have derived MyDialog class from QDialog. In MyDialog class's constructor I have connected some widget's signal and slots after setup ui. That's all for now. I use MyDıalog class from my application by;

    MyDialog *md = new MyDialog(this);
    md->show();

    When I call show method at first time my connected slot's running one by one although there isn't any signal emitted under my control which are related each connected slot. But after first show method calling this situation disappered. I mean I hide window and show again and this situation doesn't exist.
    But if I show and hide method before the signal slot connection in my constructor method there is no automatic slot running in my other show method calling.

    Why my slots running automaticaly after my first show method calling? Do you have any idea?

    Thanks in advance.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog show method's wierd problem on signal slot mechanism.

    Please post all your code, so that we don't have to guess blindfolded.

    Joh

  3. #3
    Join Date
    Jan 2010
    Location
    Ankara - TURKEY
    Posts
    30
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog show method's wierd problem on signal slot mechanism.

    Hi Joh,

    #include "settingwindow.h"
    #include "qgraph.h"
    #include <QtCore/QDebug>
    #include <QtGui/QShowEvent>
    #include <QtGui/QColorDialog>

    SettingWindow::SettingWindow(QWidget *parent)
    : QDialog(parent)
    {
    ui.setupUi(this);

    ui.groupBoxCurveSettings->hide();
    layout()->setSizeConstraint(QLayout::SetFixedSize);

    // To solve auto slot calling
    show();
    hide();

    connect(ui.radioButtonScrollAutomatic, SIGNAL(clicked(bool)), this, SLOT(setPlotScrollType(bool)));
    connect(ui.radioButtonScrollManual, SIGNAL(clicked(bool)), this, SLOT(setPlotScrollType(bool)));

    connect(ui.doubleSpinBoxYMinLeft, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYLeft(double)));
    connect(ui.doubleSpinBoxYMaxLeft, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYLeft(double)));
    connect(ui.doubleSpinBoxYStepLeft, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYLeft(double)));

    connect(ui.doubleSpinBoxYMinRight, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYRight(double)));
    connect(ui.doubleSpinBoxYMaxRight, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYRight(double)));
    connect(ui.doubleSpinBoxYStepRight, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleYRight(double)));

    connect(ui.doubleSpinBoxXStart, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleXBottom(double)));
    connect(ui.doubleSpinBoxXRange, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleXBottom(double)));
    connect(ui.doubleSpinBoxXStep, SIGNAL(valueChanged(double)), this, SLOT(setPlotAxisScaleXBottom(double)));

    connect(this, SIGNAL(panned(int, int)), parent, SLOT(setPanned(int, int)));
    connect(this, SIGNAL(plotScrollTypeChanged(const ushort)), parent, SLOT(setPlotScrollType(const ushort)));
    connect(this, SIGNAL(plotAxisScaleChanged(QwtPlot::Axis, double, double, double)),
    parent, SLOT(setPlotAxisScale(QwtPlot::Axis, double, double, double)));

    connect(ui.pushButtonDefaults, SIGNAL(clicked()), parent, SLOT(setPlotAxisScaleDefaults()));
    connect(ui.pushButtonDefaults, SIGNAL(clicked()), this, SLOT(updateAxisValues()));
    connect(ui.comboBoxCurveName, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCurveSettings()));
    connect(ui.pushButtonCurveSettings, SIGNAL(clicked()), this, SLOT(updateCurveSettings()));
    connect(ui.pushButtonCurveColorButton, SIGNAL(clicked()), this, SLOT(setCurveColor()));
    connect(ui.pushButtonCurveApply, SIGNAL(clicked()), this, SLOT(setCurveSettings()));
    }

    SettingWindow::~SettingWindow()
    {
    }

    void SettingWindow::setPlotScrollType(const bool scrollType)
    {
    QRadioButton *radioButton = qobject_cast<QRadioButton*>(sender());

    if(!radioButton)
    {
    return;
    }

    if(ui.radioButtonScrollAutomatic == radioButton)
    {
    emit plotScrollTypeChanged(0);
    emit panned(0, 0);
    }
    else if(ui.radioButtonScrollManual == radioButton)
    {
    emit plotScrollTypeChanged(1);
    emit panned(0, 0);
    }
    }

    void SettingWindow::setPlotAxisScaleYLeft(const double)
    {
    double min = ui.doubleSpinBoxYMinLeft->value();
    double max = ui.doubleSpinBoxYMaxLeft->value();
    double step = ui.doubleSpinBoxYStepLeft->value();

    emit plotAxisScaleChanged(QwtPlot::yLeft, min, max, step);
    emit panned(0, 0);
    }

    void SettingWindow::setPlotAxisScaleYRight(const double)
    {
    double min = ui.doubleSpinBoxYMinRight->value();
    double max = ui.doubleSpinBoxYMaxRight->value();
    double step = ui.doubleSpinBoxYStepRight->value();

    emit plotAxisScaleChanged(QwtPlot::yRight, min, max, step);
    emit panned(0, 0);
    }

    void SettingWindow::setPlotAxisScaleXBottom(const double)
    {
    double min = ui.doubleSpinBoxXStart->value();
    double max = min + ui.doubleSpinBoxXRange->value();
    double step = ui.doubleSpinBoxXStep->value();

    emit plotAxisScaleChanged(QwtPlot::xBottom, min, max, step);
    emit panned(0, 0);
    }

    void SettingWindow::showEvent(QShowEvent *event)
    {
    updateAxisValues();
    updateCurveValues();
    updateCurveSettings();

    QDialog::showEvent(event);
    }

    void SettingWindow::updateAxisValues()
    {
    QGraph *graph = (QGraph*)parent();

    ui.doubleSpinBoxYMinLeft->setValue(graph->plotAxisMin(QwtPlot::yLeft));
    ui.doubleSpinBoxYMaxLeft->setValue(graph->plotAxisMax(QwtPlot::yLeft));
    ui.doubleSpinBoxYStepLeft->setValue(graph->plotAxisStep(QwtPlot::yLeft));

    ui.doubleSpinBoxYMinRight->setValue(graph->plotAxisMin(QwtPlot::yRight));
    ui.doubleSpinBoxYMaxRight->setValue(graph->plotAxisMax(QwtPlot::yRight));
    ui.doubleSpinBoxYStepRight->setValue(graph->plotAxisStep(QwtPlot::yRight));

    ui.doubleSpinBoxXStart->setValue(graph->plotAxisMin(QwtPlot::xBottom));
    ui.doubleSpinBoxXRange->setValue(graph->plotAxisRange(QwtPlot::xBottom));
    ui.doubleSpinBoxXStep->setValue(graph->plotAxisStep(QwtPlot::xBottom));
    }

    void SettingWindow::updateCurveValues()
    {
    QGraph *graph = (QGraph*)parent();

    ui.comboBoxCurveName->clear();

    QwtPlotItemList plotItemList = graph->itemList(QwtPlotItem::Rtti_PlotCurve);

    for(QwtPlotItemIterator it = plotItemList.begin(); it != plotItemList.end(); it++)
    {
    QwtPlotCurve *curve = (QwtPlotCurve*)(*it);
    ui.comboBoxCurveName->addItem(curve->title().text());
    }
    }

    void SettingWindow::updateCurveSettings()
    {
    QString curveName = (QString)ui.comboBoxCurveName->currentText();

    QGraph *graph = (QGraph*)parent();
    QwtPlotItemList plotItemList = graph->itemList(QwtPlotItem::Rtti_PlotCurve);

    for(QwtPlotItemIterator it = plotItemList.begin(); it != plotItemList.end(); it++)
    {
    QwtPlotCurve *curve = (QwtPlotCurve*)(*it);
    if(curve->title().text() == curveName)
    {
    int curveStyle = (int)curve->pen().style();
    int curveWidth = (int)curve->pen().width();
    curveColor = (QColor)curve->pen().color();
    int curveAxis = (int)curve->yAxis();

    ui.comboBoxCurveStyle->setCurrentIndex(curveStyle);
    ui.spinBoxCurveWidth->setValue(curveWidth);
    ui.labelCurveColorLabel->setPalette(QPalette(curveColor));
    ui.labelCurveColorLabel->setAutoFillBackground(true);
    ui.comboBoxCurveAxis->setCurrentIndex(curveAxis);
    break;
    }
    }
    }

    void SettingWindow::setCurveColor()
    {
    QColor color = QColorDialog::getColor(Qt::green, this);

    if(color.isValid())
    {
    curveColor = color;
    ui.labelCurveColorLabel->setText(color.name());
    ui.labelCurveColorLabel->setPalette(QPalette(curveColor));
    ui.labelCurveColorLabel->setAutoFillBackground(true);
    }
    }

    void SettingWindow::setCurveSettings()
    {
    QString curveName = (QString)ui.comboBoxCurveName->currentText();
    Qt::PenStyle curveStyle = (Qt::PenStyle)ui.comboBoxCurveStyle->currentIndex();
    int curveWidth = (int)ui.spinBoxCurveWidth->value();
    QwtPlot::Axis curveAxis = (QwtPlot::Axis)ui.comboBoxCurveAxis->currentIndex();

    QGraph *graph = (QGraph*)parent();
    QwtPlotItemList plotItemList = graph->itemList(QwtPlotItem::Rtti_PlotCurve);

    for(QwtPlotItemIterator it = plotItemList.begin(); it != plotItemList.end(); it++)
    {
    QwtPlotCurve *curve = (QwtPlotCurve*)(*it);
    if(curve->title().text() == curveName)
    {
    QPen curvePen = curve->pen();
    curvePen.setStyle(curveStyle);
    curvePen.setWidth(curveWidth);
    curvePen.setColor(curveColor);
    curve->setPen(curvePen);
    curve->setYAxis(curveAxis);
    break;
    }
    }
    }

    This is my own dialog which called SettingWindow. If I call show method at first time after initialized the SettingWindow it automatically enter my all connected slots, but after I call another show methods there is no implicitly calling. To solve this problem I have added show and hide methods before connections.

    Thanks in advance.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog show method's wierd problem on signal slot mechanism.

    Hi!

    Please use the forums-code-tags! In the advanced edit there is a "#"-button!

    I think that one of your slots is called on first show, because the value has changed between initialization and then.

    Your code is not compilable as is.. so you will have to track down the source of the problem yourself. Remove the Show Hide Workaround. remove all connects - comment them out. And uncomment them one by one.

    Maybe it has to do with values you set in Designer? A plain QDoubleSpinboxes ValueChanged doesn't fire on creation..

    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QtGui>
    4. #include <QtCore>
    5.  
    6. class MainForm : public QDialog
    7. { Q_OBJECT
    8. public:
    9. MainForm() {
    10. spinbox = new QDoubleSpinBox(this);
    11. connect(spinbox,SIGNAL(valueChanged(double)),this,SLOT(SpinBoxValueChanged(double)));
    12. }
    13. protected slots:
    14. void SpinBoxValueChanged(double value) {
    15. qDebug() << "Value changed";
    16. }
    17. private:
    18. QDoubleSpinBox* spinbox;
    19. };
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QApplication app(argc, argv);
    24. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    25.  
    26. MainForm form;
    27. form.setGeometry(100,100,800,600);
    28. form.show();
    29.  
    30. return app.exec();
    31. }
    32.  
    33. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Good luck!

    Johannes

  5. The following user says thank you to JohannesMunk for this useful post:

    umituzun84 (25th March 2010)

  6. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog show method's wierd problem on signal slot mechanism.

    Aah..!! You do it yourself:

    Qt Code:
    1. void SettingWindow::showEvent(QShowEvent *event)
    2. {
    3. updateAxisValues();
    4. updateCurveValues();
    5. updateCurveSettings();
    6. QDialog::showEvent(event);
    7. }
    To copy to clipboard, switch view to plain text mode 

    On first show.. this is called.. Thus updating all values.. Thus all ValueChanged-Slots are called..

    If you want some other behaviour, you will have to change this :->

    Johannes

  7. The following user says thank you to JohannesMunk for this useful post:

    umituzun84 (25th March 2010)

  8. #6
    Join Date
    Jan 2010
    Location
    Ankara - TURKEY
    Posts
    30
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog show method's wierd problem on signal slot mechanism.

    Hi Johannes,

    I have realized the explicit value change on controls in my update functions How dummy I am! Thanks so much for your helps. I solved this problem by kind of this codes;

    Qt Code:
    1. ui.comboBoxCurveName->blockSignals(true); // For stop signal creation
    2. ui.comboBoxCurveName->clear();
    3.  
    4. QwtPlotItemList plotItemList = graph->itemList(QwtPlotItem::Rtti_PlotCurve);
    5.  
    6. for(QwtPlotItemIterator it = plotItemList.begin(); it != plotItemList.end(); it++)
    7. {
    8. QwtPlotCurve *curve = (QwtPlotCurve*)(*it);
    9. ui.comboBoxCurveName->addItem(curve->title().text());
    10. }
    11. ui.comboBoxCurveName->blockSignals(false); // For start signal creation
    To copy to clipboard, switch view to plain text mode 

    So there is no signal while I am manipulating controls attributes.

    Thanks again.
    Regards.

Similar Threads

  1. When should I use signal/slot mechanism
    By Bryku in forum Newbie
    Replies: 3
    Last Post: 10th February 2010, 22:24
  2. slow signal-slot-mechanism
    By blm in forum Qt Programming
    Replies: 11
    Last Post: 28th October 2008, 17:10
  3. can a single statement work in signal/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2008, 08:24
  4. The threaded signal/slot mechanism
    By xbtl in forum Newbie
    Replies: 1
    Last Post: 30th March 2008, 00:07
  5. Replies: 4
    Last Post: 23rd January 2006, 16:51

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.