GUI Button Deactivation



  • Hello Qt Community,
    my name is Stefan and I am a C/C++/Java Programmer. My newest Projekt is an Programm for Distribution, the Programm is working but it has some mistakes and there are functions which are missed. So they came to my and hopefully i can help them.

    But my First Problems came into Mind when i want to change the following:

    A Picture of the programm now:

    [URL=http://imageshack.us/photo/my-images/339/importexport.jpg/][IMG]http://img339.imageshack.us/img339/2951/importexport.jpg[/IMG][/URL]

    This part should only allow to include/exclude Data when possible, because if it allowes it always it crashes.

    @#include "ClassExclusionDialog.h"

    ClassExclusionDialog::ClassExclusionDialog( const QList<QString>& classes, QWidget *parent ) : QDialog( parent )
    {
    m_allClasses = new QListWidget();
    m_excludedClasses = new QListWidget();
    m_excludeButton = new QPushButton( tr( "Exclude" ) );
    m_includeButton = new QPushButton( tr( "Include" ) );
    m_okButton = new QPushButton( tr( "Ok" ) );

    m_allClasses->addItems( classes );
    m_allClasses->setSelectionMode( QAbstractItemView::SingleSelection );
    m_allClasses->sortItems();
    m_excludedClasses->setSelectionMode( QAbstractItemView::SingleSelection );

    connect( m_okButton, SIGNAL( clicked() ), this, SLOT( close() ) );
    connect( m_excludeButton, SIGNAL( clicked() ), this, SLOT( excludeClass() ) );
    connect( m_includeButton, SIGNAL( clicked() ), this, SLOT( includeClass() ) );

    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->addWidget( new QLabel( tr( "Choose which classes to exclude during the import." ) ) );
    QHBoxLayout *hLayout = new QHBoxLayout();
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addWidget( m_allClasses );
    leftLayout->addWidget( m_excludeButton );
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addWidget( m_excludedClasses );
    rightLayout->addWidget( m_includeButton );
    hLayout->addLayout( leftLayout );
    hLayout->addLayout( rightLayout );
    vLayout->addLayout( hLayout );
    hLayout = new QHBoxLayout();
    hLayout->addStretch();
    hLayout->addWidget( m_okButton );
    vLayout->addLayout( hLayout );

    setModal( true );
    setLayout( vLayout );
    }

    ClassExclusionDialog::~ClassExclusionDialog()
    {
    delete m_okButton;
    delete m_includeButton;
    delete m_excludeButton;
    delete m_excludedClasses;
    delete m_allClasses;
    }

    QList<QString> ClassExclusionDialog::selection() const
    {
    QList<QString> classes;
    for( int i = 0; i < m_excludedClasses->count(); i++ )//{
    // if(m_excludedClasses->count()<0) //
    // return 0;//
    classes.push_back( m_excludedClasses->item( i )->text() );
    // }
    return classes;
    }

    void ClassExclusionDialog::includeClass()
    {
    // if(m_allClasses->selectedItems().count() == null){
    // m_includeButton->setDisabled(true); //Here i tryed the same
    // m_includeButton->repaint();
    //}
    // else{
    QString text = m_excludedClasses->removeItemWidget( m_excludedClasses->selectedItems().first() ); //War im Original auskommentiert
    // m_excludedClasses->selectedItems().first()->text();
    delete m_excludedClasses->takeItem( m_excludedClasses->currentRow() );
    m_allClasses->addItem( text );
    m_allClasses->sortItems();
    }
    }

    void ClassExclusionDialog::excludeClass()
    {

    // if(m_excludedClasses->selectedItems().count() == null){
    // m_excludeButton->setDisabled(true); This was my try to deaktivate the button when nessesary
    // m_excludeButton->repaint();

    // else{

    QString text = m_allClasses->removeItemWidget( m_allClasses->selectedItems().first() );
    

    //m_allClasses->selectedItems().first()->text();
    delete m_allClasses->takeItem( m_allClasses->currentRow() );
    m_excludedClasses->addItem( text );
    m_excludedClasses->sortItems();
    }
    }
    @

    Im looking forward to your answer.

    Stefan



  • Hi, Im not sure if it solves your problem but this is not neccessary:
    @delete m_okButton;
    delete m_includeButton;
    delete m_excludeButton;
    delete m_excludedClasses;
    delete m_allClasses;@

    You dont need to delete QObjects that have a parent, the parent deletes its children automatically in its destructor. You made all of your gui items to be ClassExclusionDialog's children by adding them to layout which you set to ClassExclusionDialog widget.

    Have a look at http://doc.qt.digia.com/qt/objecttrees.html



  • Hi,
    this code looks really ugly.

    First, I would suggest that you use update() instead of repaint().
    Second, It is better to use preincrement in a for loop then postincrement.

    Here is how I would write the code. It is better to read and better to debug to find the bug which you are looking for.

    @void ClassExclusionDialog::includeClass()
    {
    if( !m_allClasses->selectedItems().isEmpty() )
    {
    QListWidgetItem* tmpItem = m_excludedClasses->selectedItems().first();

       m_allClasses->addItem( tmpItem->text(); );
       m_allClasses->sortItems();
    
        m_excludedClasses->removeItemWidget( tmpItem );
       // delete tmpItem; // I don't think that this is necessary
    

    }
    else
    {
    m_includeButton->setDisabled(true);
    }
    }@



  • I made some mistakes in the old post.

    @
    #include "ClassExclusionDialog.h"

    ClassExclusionDialog::ClassExclusionDialog( const QList<QString>& classes, QWidget *parent ) : QDialog( parent )
    {
    m_allClasses = new QListWidget();
    m_excludedClasses = new QListWidget();
    m_excludeButton = new QPushButton( tr( "Exclude" ) );
    m_includeButton = new QPushButton( tr( "Include" ) );
    m_okButton = new QPushButton( tr( "Ok" ) );

    m_allClasses->addItems( classes );
    m_allClasses->setSelectionMode( QAbstractItemView::SingleSelection );
    m_allClasses->sortItems();
    m_excludedClasses->setSelectionMode( QAbstractItemView::SingleSelection );

    connect( m_okButton, SIGNAL( clicked() ), this, SLOT( close() ) );
    connect( m_excludeButton, SIGNAL( clicked() ), this, SLOT( excludeClass() ) );
    connect( m_includeButton, SIGNAL( clicked() ), this, SLOT( includeClass() ) );

    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->addWidget( new QLabel( tr( "Choose which classes to exclude during the import." ) ) );
    QHBoxLayout *hLayout = new QHBoxLayout();
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addWidget( m_allClasses );
    leftLayout->addWidget( m_excludeButton );
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addWidget( m_excludedClasses );
    rightLayout->addWidget( m_includeButton );
    hLayout->addLayout( leftLayout );
    hLayout->addLayout( rightLayout );
    vLayout->addLayout( hLayout );
    hLayout = new QHBoxLayout();
    hLayout->addStretch();
    hLayout->addWidget( m_okButton );
    vLayout->addLayout( hLayout );

    setModal( true );
    setLayout( vLayout );
    }

    ClassExclusionDialog::~ClassExclusionDialog()
    {
    delete m_okButton;
    delete m_includeButton;
    delete m_excludeButton;
    delete m_excludedClasses;
    delete m_allClasses;
    }

    void ClassExclusionDialog::includeClass()
    {
    if( !m_allClasses->selectedItems().isEmpty() )
    {
    QListWidgetItem* tmpItem = m_excludedClasses->selectedItems().first();

       m_allClasses->addItem( tmpItem->text());
       m_allClasses->sortItems();
    
        m_excludedClasses->removeItemWidget( tmpItem );
       delete tmpItem; // I don't think that this is necessary
    

    }
    else
    {
    m_includeButton->setDisabled(true);
    }
    }

    void ClassExclusionDialog::excludeClass()
    {
    if(!m_excludedClasses->selectedItems().isEmpty()){
    QListWidgetItem* tmpItem = m_allClasses->selectedItems().first();
    if(tmpItem != NULL){ //Tring to check if it is null, because maybe it causes the problem
    m_excludedClasses->addItem(tmpItem->text());
    m_excludedClasses->sortItems();
    m_allClasses->removeItemWidget(tmpItem);
    }

        delete tmpItem;
    }
    else{
        m_excludeButton->setDisabled(true);
    
    }
    

    }

    @

    http://www.bilderload.com/bild/279542/compilererrorAWXXF.jpg



  • Can somebody help me?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.