Results 1 to 11 of 11

Thread: Circular dependency fix/alternative?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Circular dependency fix/alternative?

    Wow, no offense, but that's a way more confusing implementation that it needs to be. No wonder you are having trouble with circular dependencies.

    I think most of your problem stems from the fact that there is no one "in charge" in your GUI. You have a tabbed dialog (a QWidget, actually - ColorSelect) that contains two tabs, neither of which knows about the other but which need to communicate information. And probably, because you couldn't figure out how to do it in Qt Designer, you create and show() your ColorWheel instance separately.

    I am going to be presumptuous and refactor your code a bit to make it less interdependent.

    First step is to make ColorWheel a proper and independent child of the ColorSelect widget, by "promoting" the QWidget you have created in Designer. To do this, you open the ui file in Designer, right-click on the place where you inserted the QWidget instead of the ColorWheel, and select "Promote to...". This will open the widget promotion dialog. In the Promoted class name box, type "ColorWheel" and it will automatically insert the header file name in the box below. Click "Add", then click Promote and Close. If you inspect the object list, you will now see that the object type for the color wheel is now ColorWheel instead of QWidget.

    Next step is to get rid of the dependence of ColorWheel on ColorSelect. Remove the ColorSelect argument from the constructor, remove the member variable, and remove all references to it from the ColorWheel .cpp and .h files. Remove the include of colorselect.h. Replace the call to colorSelect->updatePalette() in the ColorWheel class with a call to emit colorChange(). Move the QPalette stuff out of main() and into the ColorWheel constructor:

    Qt Code:
    1. ColorWheel::ColorWheel(QWidget *parent) :
    2. QWidget(parent),
    3. initSize(200,200),
    4. mouseDown(false),
    5. margin(0),
    6. wheelWidth(30),
    7. current(Qt::red),
    8. inWheel(false),
    9. inSquare(false)
    10. {
    11. // resize(initSize);
    12. current = current.toHsv();
    13. // setMinimumSize(200,200);
    14. setCursor(Qt::CrossCursor);
    15.  
    16. QPalette wheelPalette = palette();
    17. wheelPalette.setBrush(QPalette::Window, QBrush(Qt::white));
    18. setPalette(wheelPalette);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now, ColorWheel is completely independent of ColorSelect. You need to reconnect the notification about color changes in the wheel so that ColorSelect can follow them. In the ColorSelect constructor, add a connect statement:

    Qt Code:
    1. connect( ui->colorWheelWidget, SIGNAL( colorChange( const QColor & ) ), this, SLOT( updatePalette( const QColor & ) ) );
    To copy to clipboard, switch view to plain text mode 

    and rewrite the updatePalette() method as a slot:

    Qt Code:
    1. // .h:
    2.  
    3. private slots:
    4. void updatePalette( const QColor & );
    5.  
    6. // .cpp:
    7.  
    8. void ColorSelect::updatePalette( const QColor & color )
    9. {
    10. //get palette
    11. QPalette pal = ui->colorPalette->palette();
    12.  
    13. //update palette with colorToRGB
    14. pal.setColor(QPalette::Window, color);
    15.  
    16. ui->colorPalette->setPalette(pal);
    17. emit colorChanged(color);
    18. qDebug()<< color;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Next, now that you are building the ColorWheel entirely within ColorSelect, you can dramatically simplify main():

    Qt Code:
    1. #include "colorselect.h"
    2. #include "serialcom.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. SerialCom *serialCom = new SerialCom();
    8. ColorSelect *colorSelect = new ColorSelect(0,serialCom);
    9. colorSelect->setWindowTitle("RGB-LED Color Picker");
    10.  
    11. //show UI
    12. colorSelect->show();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Next step is to break the dependence of DeviceDialog on ColorSelect. DeviceDialog's entire purpose is to capture two pieces of text from the user. So, it has no business knowing that ColorSelect is going to add those things to a list widget somewhere. It just simply needs to announce to the world, "Hey, I have new text!" So, DeviceDialog becomes this:

    Qt Code:
    1. // .h:
    2. #ifndef DEVICEDIALOG_H
    3. #define DEVICEDIALOG_H
    4.  
    5. #include <QDialog>
    6.  
    7. namespace Ui {
    8. class DeviceDialog;
    9. }
    10.  
    11. class DeviceDialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit DeviceDialog(QWidget *parent = 0);
    17. void setLineEdits(QString);
    18. ~DeviceDialog();
    19.  
    20. signals:
    21. void houseAndUnitChanged( const QString & house, const QString & unit );
    22.  
    23. private slots:
    24. void on_buttonBox_accepted();
    25.  
    26. private:
    27. Ui::DeviceDialog *ui;
    28. };
    29.  
    30. #endif // DEVICEDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // .cpp
    2.  
    3. #include "devicedialog.h"
    4. #include "ui_devicedialog.h"
    5.  
    6. DeviceDialog::DeviceDialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::DeviceDialog)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. DeviceDialog::~DeviceDialog()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void DeviceDialog::setLineEdits(QString houseAndUnit)
    19. {
    20. ui->houseLineEdit->setText(houseAndUnit.mid(0,1));
    21. ui->unitLineEdit->setText(houseAndUnit.mid(1,1));
    22. }
    23.  
    24. void DeviceDialog::on_buttonBox_accepted()
    25. {
    26. emit houseAndUnitChanged( (ui->houseLineEdit->text(), ui->unitLineEdit->text() );
    27. }
    To copy to clipboard, switch view to plain text mode 

    Finally, you have to fix up ColorSelect so it handles the DeviceDialog signal:

    Qt Code:
    1. // .h
    2.  
    3. private slots:
    4. void onHouseAndUnitChanged( const QString & house, const QString & unit );
    5.  
    6. // and delete the "DeviceDialog" member variable.
    7.  
    8. // .cpp
    9.  
    10. void ColorSelect::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
    11. {
    12. DeviceDialog dialog(this);
    13.  
    14. connect( &dialog, SIGNAL( houseAndUnitChanged( const QString &, const QString & ) ), this, SLOT( onHouseAndUnitChanged( const QString &, const QString & ) ) );
    15. dialog.setLineEdits(item->data(Qt::UserRole).toString());
    16. dialog.exec();
    17. }
    18.  
    19. void ColorSelect::onHouseAndUnitChanged( const QString & house, const QString & unit )
    20. {
    21. setItemData( house, unit );
    22. }
    To copy to clipboard, switch view to plain text mode 


    There's still more cleanup and refactoring you could do, but now you have something where there are no circular dependencies, and in fact very few dependencies at all.

    I've attached a zip file with all the code changes.

    colorselect_serial.zip

    By the way, I am out of the office until the end of the week, so I won't be following the forum.

  2. The following user says thank you to d_stranz for this useful post:

    Leutzig (1st December 2015)

Similar Threads

  1. Circular Layout
    By Leolander in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2010, 08:02
  2. Circular QLinkedList
    By dyngoman in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2010, 08:57
  3. circular buttons
    By moabi in forum Newbie
    Replies: 2
    Last Post: 18th March 2010, 04:28
  4. Circular Linklist.
    By AmolShinde_8 in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 04:23
  5. Circular scale
    By fruzzo in forum Qwt
    Replies: 1
    Last Post: 6th March 2008, 07:20

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.