Results 1 to 2 of 2

Thread: Propagation of properties on a changing widget

  1. #1
    Join Date
    Feb 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Propagation of properties on a changing widget

    Hello,

    I'm working on a project (coded in Java, using Qt Jambi) in which I need the main window to show different widgets according to the user's actions. The main window has a QSplitter as its central widget, a QListView to the left of said QSplitter and a QWidget to the right. Now, when the user clicks on an item from the QListView, I have that signal connected to a slot in which I call functions from different classes that return a QWidget, and assign it to the QWidget of the main window so it shows the appropriate stuff according to what the user selected.
    My problem is that, when selecting one of the options, I have to show a QGraphicsView and draw a certain figure at different points of the QGraphicsView (or rather, the QGraphicsScene) and with different sizes every X seconds, deleting the previous figure so there's at most one figure at any given time (currently I only draw one, though, since I haven't coded the "every X seconds" part yet). But no matter what I do, the figure always shows up at the same spot, with the same size.

    The size is controlled by the value of a QSpinBox (which is part of a QWidget that is assigned to the main window's "variable QWidget" that I mentioned before), with its "valueChanged" signal connected to a slot that stores the value of the QSpinBox in a variable which is then used to draw the figure. But when I select a different item in the QListView (for example, when I click "draw figure" after having clicked "configure size"), it seems that either the value of the QSpinBox is changed to zero (and thus the variable that stores the size also changes to zero) or the change is lost when the QWidget changes and shows the QGraphicsView instead of the QSpinBox. The result is that, even if the figure is drawn, the user can't see it since it has a size of zero.

    Relevant code is as follows:

    Qt Code:
    1. public void showWidget() {
    2. if (listView.currentItem().text().equalsIgnoreCase(tr("Configure"))) {
    3. configModeOn = true;
    4. variableWidget.show();
    5. listView.currentItem().setHidden(true);
    6. listView.addItem("Back");
    7. } else if (listView.currentItem().text().equalsIgnoreCase(tr("Back"))) {
    8. configModeOn = false;
    9. variableWidget.show();
    10. listView.currentItem().setHidden(true);
    11. listView.addItem("Configure");
    12. } else {
    13. try {
    14. Class<?> p = Class.forName("plugin."+listView.currentItem().text());
    15. PluginInterface plugin = (PluginInterface) p.newInstance();
    16. if (configModeOn){
    17. setVariableWidget(plugin.showConfiguration());
    18. } else {
    19. setVariableWidget(plugin.showTest());
    20. }
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    This is the method from the MainWindow class that updates the changing QWidget according to what the user selects in the QListView. As you can see, it just shows different stuff (returned by methods of classes that implement the PluginInterface interface, this is necessary because the application should be extensible via plugins stored in jar files) depending on whether the user is configuring the app or not. The methods from the class that show the QGraphicsView and the QSpinBox are as follows:

    Qt Code:
    1. public QWidget showConfiguration() {
    2. QVBoxLayout mainLayout = new QVBoxLayout();
    3. visualStimulusButton.setChecked(true);
    4. visualStimulusButton.clicked.connect(this, "changeToVisual()");
    5. auditiveStimulusButton.clicked.connect(this, "changeToAuditive()");
    6. bothStimulusButton.clicked.connect(this, "changeToBoth()");
    7. QPushButton browseButton = new QPushButton("&Browse");
    8. browseButton.clicked.connect(this, "browse()");
    9.  
    10. QHBoxLayout sizeLayout = new QHBoxLayout();
    11. QLabel label = new QLabel("Size of the target");
    12. box.valueChanged.connect(this, "updateSize()");
    13. sizeLayout.addWidget(label);
    14. sizeLayout.addWidget(box);
    15.  
    16. QDialogButtonBox buttonBox = new QDialogButtonBox(Qt.Orientation.Horizontal);
    17. buttonBox.addButton(visualStimulusButton, QDialogButtonBox.ButtonRole.ActionRole);
    18. buttonBox.addButton(auditiveStimulusButton, QDialogButtonBox.ButtonRole.ActionRole);
    19. buttonBox.addButton(bothStimulusButton, QDialogButtonBox.ButtonRole.ActionRole);
    20. mainLayout.addWidget(buttonBox);
    21.  
    22. QHBoxLayout bottomLayout = new QHBoxLayout();
    23. bottomLayout.addWidget(fileComboBox);
    24. bottomLayout.addWidget(browseButton);
    25. mainLayout.addLayout(sizeLayout);
    26. mainLayout.addLayout(bottomLayout);
    27. config.setLayout(mainLayout);
    28. return config;
    29. }
    To copy to clipboard, switch view to plain text mode 

    That's the configuration widget, which has the spinbox that's giving me headaches. This is the one with the QGraphicsView:

    Qt Code:
    1. public QWidget showTest() {
    2. System.out.println("Using size: "+size);
    3. QGraphicsScene scene = new QGraphicsScene(test);
    4. QGraphicsEllipseItem circle = new QGraphicsEllipseItem(10, 100, size,size);
    5. QHBoxLayout buttonLayout = new QHBoxLayout();
    6. QColor black = new QColor(0,0,0,255);
    7. QPen pen = new QPen(black);
    8. QColor red = new QColor(255, 0, 0, 255);
    9. QBrush brush = new QBrush(red);
    10. circle.setPen(pen);
    11. circle.setBrush(brush);
    12. scene.addItem(circle);
    13. QVBoxLayout mainLayout = new QVBoxLayout();
    14.  
    15. testButton = new QPushButton("&Take the Test", test);
    16. testButton.setDefault(true);
    17. trainingButton = new QPushButton("&Practice", test);
    18. trainingButton.setCheckable(true);
    19. trainingButton.setAutoDefault(false);
    20. buttonLayout.addWidget(testButton);
    21. buttonLayout.addWidget(trainingButton);
    22. testButton.clicked.connect(this, "changeMode()");
    23. trainingButton.clicked.connect(this, "changeModeTraining()");
    24.  
    25. QGraphicsView view = new QGraphicsView(scene, test);
    26. view.resize(400, 300);
    27.  
    28. mainLayout.addWidget(view);
    29. mainLayout.addLayout(buttonLayout);
    30.  
    31. return test;
    32. }
    To copy to clipboard, switch view to plain text mode 

    Nevermind the QPushButtons in this last snippet of code, for some reason they aren't shown either, just the QGraphicsView. But the important thing here is that the figure always appears with the same size (I have size declared as "private int size;", without explicitly initializing, and only change its value when the value of the spinbox changes, which produces the effect of the circle being drawn with size 0; but if I initialize it at, say, 50 (that is, declaring it as "private int size = 50;") the circle is drawn with size 50, regardless of the value of "size" and/or the value of the spinbox), and in the same spot (the center of the QGraphicsView) regardless of the first two parameters being (10, 100) or (X, Y) for any value of X and Y.

    Now, I know that the problem is exactly between the keyboard and the chair, and I'm positive that changing the variable QWidget as a result of selecting a different item in the QListView (therefore not showing the QSpinBox anymore, and showing the QGraphicsView) is what resets the size to zero somehow. But I don't see why, nor a proper way of solving it. Any ideas on what I'm doing wrong and/or how to do it right?

    P.S: The "updateSize()" slot is as easy as "size=box.value()", so I doubt that's the source of the problem.

  2. #2
    Join Date
    Feb 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Propagation of properties on a changing widget

    Problem located (but unsolved), and boy am I a noob. As expected, since I'm instantiating the plugin every thime "showWidget()" gets called (by doing "PluginInterface plugin = (PluginInterface) p.newInstance();"), the values used are the initial ones. I guess the solution is to store them somewhere else (a config file, for example) and read them upon instantiation of the plugin, or maybe using the Singleton pattern (though I don't think I can use it given the circumstances).

    The other issues (buttons not showing in the Qwidget that has the QGraphicsVie, and figure being drawn always in the same spot) remain. Any idea why that happens?
    Last edited by qt_noob; 8th February 2009 at 11:53.

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  3. Stacked widget mouse propagation
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 17:54
  4. Replies: 5
    Last Post: 16th May 2006, 20:38
  5. How to initialize properties to a custom plugin widget?
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2006, 16:56

Tags for this Thread

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.