Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is the most simple radio button dialog?



  • I need a simple right click dialog that provides three options to the user. I am working on a government project Qt3 in Linux. Yes I know, 16 years ago, but it works and the current managers don’t want to be the ones to spend more than a labor year in the upgrade. Keeping it simple, there are two, make that three problems.

    1. Other apps that use radio buttons are so convoluted I cannot make sense of them. The simple one goes 5 layers deep.
    2. In this app the radio buttons are all on top of each other.
    3. On start this error is presented for each radio button:
      QLayout: Adding QRadioButton/m_rd_normalize_to_max_input (child of QButtonGroup/m_button_group) to layout for C_Dialog_Select_Normalize/unnamed
      I cannot understand what that message is telling me. I don’t see that anything is unnamed.

    Understanding Qt3 is really old, please just try to tell me the basic structure and i will go from that. It is that basic concept of laying out these radio buttons that I have yet to grasp.

    Here is my latest dot H file

    /* dialog_select_normalize.h
     * Provide the ability to select 1 of N normalize modes
     * Implemented via a set of radio buttons
     * ***************************************************** */
    #ifndef DIALOG_SELECT_SIMPLE_H
    #define DIALOG_SELECT_SIMPLE_H
    
    #include <qvbuttongroup.h>
    #include <qlabel.h>
    #include <qradiobutton.h>
    #include <qlayout.h> 
    
    #include "constants.h"
    
    class C_Dialog_Select_Normalize  : public QWidget
    {
       Q_OBJECT
    public:
       C_Dialog_Select_Normalize( QWidget * parent = 0  );
    
    signals:
       void signal_normalize_mode( int );
    
    public slots:
       void slot_button_clicked( int  );
    private:
    
       QVBoxLayout  * m_vertical_box;
       QButtonGroup * m_button_group;
    
       QRadioButton * m_rb_normalize_to_max_input;
       QRadioButton * m_rb_normalize_to_one;
       QRadioButton * m_rb_disable_normalize;
    
    };
    #endif
    

    Here is the latest dot cpp file

    /* *****************************************************************************
     * dialog_select_normalize.cpp
     * Provide the ability to select 1 of N normalize modes
     * ************************************************************************** */
    
    #include <qlayout.h>
    #include <qtooltip.h>
    
    #include "constants.h"
    #include "dialog_select_normalize.h"
    
    C_Dialog_Select_Normalize::C_Dialog_Select_Normalize(  QWidget * parent )
        : QWidget( parent )  
    {
       const QString MY_NAME = "C_Dialog_Select_Normalize::C_Dialog_Select_Normalize()";
        
       setCaption( DIALOG_CAPTIONS );
    
       m_vertical_box = new QVBoxLayout(  this, 
                                          6,    // margin
                                          1,    // spacing
                                          "m_vertical_box" );
       
       m_button_group = new QButtonGroup( 3,   // strips (or rows in this case )
                                          Qt::Vertical,
                                          "m_button_group_title",
                                          this,
                                          "m_button_group" );
    
       m_button_group->setExclusive( true );
    
          // parent is the button group in order to become mutually exclusive
       m_rb_normalize_to_max_input = new QRadioButton( "Max Input", m_button_group, "m_rd_normalize_to_max_input" );
       m_rb_normalize_to_one =       new QRadioButton( "Max = 1",   m_button_group, "m_rd_normalize_to_one" );
       m_rb_disable_normalize =      new QRadioButton( "Disable",   m_button_group, "m_la_dialog_description" );
       
       m_rb_normalize_to_max_input->setChecked( true );
     
       QToolTip::add( m_rb_normalize_to_max_input, "The maximum input value becomes the maximum value displayed." );
       QToolTip::add( m_rb_normalize_to_one,       "Maximum value displayed = 1."  );
       QToolTip::add( m_rb_disable_normalize,      "DFT outputs amplitudes are displayed unchanged" );
    
          // add to the vertical box for layout
       m_vertical_box->addWidget( m_rb_normalize_to_max_input );
       m_vertical_box->addWidget( m_rb_normalize_to_one );
       m_vertical_box->addWidget( m_rb_disable_normalize );
    
    // .............................................................................
    // Put the local connects here at the end of the constructor
    // .............................................................................
    
       connect( m_button_group, SIGNAL( clicked( int ) ), this, SLOT( slot_button_clicked( int ) ));
    
    }; // end of constructor
    
    // ........................................................
    // SLOT
    // ........................................................
    
    void C_Dialog_Select_Normalize::slot_button_clicked( int new_value )
    {
    const QString MY_NAME = "C_Dialog_Select_Normalize::slot_button_clicked";
      printf( "\n%4d %s() new_value %d ", __LINE__, MY_NAME.ascii(), new_value );
       
      emit signal_normalize_mode( new_value );
    }
    

  • Lifetime Qt Champion

    Hi,

    Do you get the same message if you don't set the parent on the layout construction and call setLayout once you're done with the button setup ?



  • @SGaist said in What is the most simple radio button dialog?:

    Hi,

    Do you get the same message if you don't set the parent on the layout construction and call setLayout once you're done with the button setup ?

    I don't follow your meaning. Do you mean to omit the "this" in one or both of the new statement for the QVBoxLayout and the QButtonGroup? I visited the Qt Assistant and did not find ->setLayout() for either one.

    In the meantime, I have tried replacing the QVBoxLayout with QGridLayout. But everything is still squished together until adding some ->setRowSpacing( 0, 30 ) and setColSpacing( 0, 300 ) The button group seems to occupy only one row. The crux seems to be that there is no way to set the size or spacing of the ButtonGroup. Understandable. But, everything in the button group seems to be squished together. In my mind, that makes it clear that I am missing an important concept.

    Edit: That "Understandable." refers to the concept that the ButtonGroup is just to associate the multiple radio buttons so they may be made exclusive. I suspect that perception is incorrect, or maybe partially correct.
    EDIT
    Switching the first object to QGridLayout has helped and produced something workable.
    Except: For the grid layout I must use:

    m_grid_layout = new QGridLayout(  this, 
                                       3,    // rows
                                       1,    // columns
                                       6,    // margins
                                       6,    // spacing
                                       "m_grid_layout" );
    m_grid_layout ->setRowSpacing( 0, 30 );
    m_grid_layout ->setRowSpacing( 1, 30 );
    m_grid_layout ->setRowSpacing( 2, 30 );
    m_grid_layout ->setColSpacing( 0, 300 );
    

    And, for the button group I must use

    QPoint top_left(    0,   0 );
    QPoint bot_right( 200, 400 );
    QRect button_geometry( top_left, bot_right );
    m_button_group->setGeometry( button_geometry );
    

    Omitting any of the set row or geometry settings results in only the last button being visible. I find it odd that both must be used.
    Again, I keep thinking there is a fundamental concept that I have yet to grasp.

    Edit Again

    I have something that is workable and more simple.
    Eliminate the QGridLayout. That caused the dialog to take most of the vertical distance of the monitor and all of the width.
    Then add

    setMaximumWidth( 200 );
    setMaximumHeight( 100 );
    

    These are the first two lines in the constructor and go directly to the top level QWidget.
    I retained the set geometry for the QButtonGroup, that seems to prevent each radio buttons from overwriting the previous one.
    Now I only have the QWidget from which the class inherits and the QButtonGroup to collect the radio buttons and make them mutually exclusive.
    This is Qt3 and probably no one cares, nor should they. But posting and getting the feeling that I am conversing with someone has made this trip easier.
    I will ask this: What is the difference between: setExclusive( true ) and setRadioButtonExclusive( true )?


  • Lifetime Qt Champion

    Haaaa ! I found the issue...

    Qt 3's QButtonGroup is a widget already. Hence the error you are seeing. Take a look at the existing subclasses for pre-made vertical and horizontal layouts. Use it directly, no need for a dedicated layout in a dedicated widget.

    As for the properties, one is dedicated to QRadioButton and the other for the other button classes.



  • @SGaist
    Wow, thank you for taking the time to consider my problem and seeing the error.
    Regarding: setExclusive( true ) and setRadioButtonExclusive( true )
    I commented them out one at a time, then both. The radio buttons remain mutually exclusive. Now I am pondering if I should leave one it the code because I might be doing something strange and that one line of might help someone who needs to modify this.

    I greatly appreciate you taking the time to read, consider, and reply. That helps me technically and emotionally.
    Thank you for your time.


Log in to reply