[SOLVED] QToolButton: not able to enable or disable button in my function
-
Hello,
I am currently trying to enable and disable certain toolbar buttons in my app. I am able to enable and disable them when I am in my constructor for the app, but when I call a separate function to handle this it builds fine, but I get an exception when I try to run it. My code is as follow:
Constructor:
@UI_3::UI_3(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UI_3)
{
ui->setupUi(this);/*Create the tool bar and add it to the main window*/ QToolBar *toolBar = new QToolBar(this); toolBar->setFloatable(false); /*Do not allow the toolbar to be undockable*/ toolBar->setMovable(false); /*Do not allow the toolbar to be moved*/ addToolBar(Qt::LeftToolBarArea, toolBar); /*Add tool bar to the main window*/ /*Create buttons and display icon/tect for each*/ QToolButton *onButton = new QToolButton(toolBar); onButton->setIcon(QIcon("C:/Qt/Projects/CNCUI_3AXIS/icons/yes.png")); onButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); onButton->setText("NC On"); QToolButton *offButton = new QToolButton(toolBar); offButton->setIcon(QIcon("C:/Qt/Projects/CNCUI_3AXIS/icons/no.png")); offButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); offButton->setText("NC Off"); QToolButton *openFileButton = new QToolButton(toolBar); openFileButton->setIcon(QIcon("C:/Qt/Projects/CNCUI_3AXIS/icons/directory_open.png")); openFileButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); openFileButton->setText("Open"); QToolButton *runButton = new QToolButton(toolBar); runButton->setIcon(QIcon("C:/Qt/Projects/CNCUI_3AXIS/icons/arrow_green.png")); runButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); runButton->setText("Run"); QToolButton *stopButton = new QToolButton(toolBar); stopButton->setIcon(QIcon("C:/Qt/Projects/CNCUI_3AXIS/icons/error.png")); stopButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); stopButton->setText("Stop"); /*Add buttons and separators to toolbar*/ toolBar->addWidget(onButton); toolBar->addSeparator(); toolBar->addWidget(offButton); toolBar->addSeparator(); toolBar->addWidget(openFileButton); toolBar->addSeparator(); toolBar->addWidget(runButton); toolBar->addSeparator(); toolBar->addWidget(stopButton);
...
onButton->setEnabled(true);
offButton->setEnabled(false);
openFileButton->setEnabled(false);
runButton->setEnabled(false);
stopButton->setEnabled(false);
...
}@Function that doesnt allow me to disable and enable toolbar buttons:
@void UI_3::setModeFocusFor(MODE_FOCUS mode)
{
switch(mode)
{
case(MANUAL):
/Only enable manual tab/
ui->manualTab->setEnabled(true);
ui->editTab->setEnabled(false);
ui->autoTab->setEnabled(false);
ui->plotTab->setEnabled(false);
ui->mdiTab->setEnabled(false);
/Only enable manual toolbar buttons/
onButton->setEnabled(false);
offButton->setEnabled(true);
openFileButton->setEnabled(false);
runButton->setEnabled(false);
stopButton->setEnabled(false);
break;
case(EDIT):
/Only enable edit tab/
ui->editTab->setEnabled(true);
ui->autoTab->setEnabled(false);
ui->manualTab->setEnabled(false);
ui->plotTab->setEnabled(false);
ui->mdiTab->setEnabled(false);
/Only enable edit toolbar buttons/
onButton->setEnabled(false);
offButton->setEnabled(true);
openFileButton->setEnabled(true);
runButton->setEnabled(false);
stopButton->setEnabled(false);
break;
}
}
@The exception occurs right at the first onButton->setEnabled(true) call.
The class in the header file is as follows:
@class UI_3 : public QMainWindow
{
Q_OBJECTpublic:
explicit UI_3(QWidget *parent = 0);
~UI_3();private slots:
void on_tabWidget_currentChanged(QWidget *arg1); /Slot for handling tab mode changes/
void setModeFocusFor(MODE_FOCUS mode);
/Slots for handling toolbar button clicks/
void onButton_clicked();
void offButton_clicked();
void openFileButton_clicked();
void runButton_clicked();
void stopButton_clicked();private:
Ui::UI_3 *ui;
QToolBar *toolBar; /Main window tool bar/
QToolButton *onButton; /Button for turning NC on/
QToolButton *offButton; /Button for turning NC off/
QToolButton *openFileButton; /Button for loading a part program file/
QToolButton *runButton; /Button for running a program/
QToolButton *stopButton; /Button for stopping a program/};@
Any help would be great. Thanks!
-
I solved the problem. The error was in the following:
@ QToolBar *toolBar = new QToolBar(this);
QToolButton *onButton = new QToolButton(toolBar); QToolButton *offButton = new QToolButton(toolBar); QToolButton *openFileButton = new QToolButton(toolBar); QToolButton *runButton = new QToolButton(toolBar); QToolButton *stopButton = new QToolButton(toolBar);@
It should be:
@ toolBar = new QToolBar(this);
onButton = new QToolButton(toolBar); offButton = new QToolButton(toolBar); openFileButton = new QToolButton(toolBar); runButton = new QToolButton(toolBar); stopButton = new QToolButton(toolBar);@
because the pointers were already instantiated in the header file so I was doing it twice by accident and that made it throw an exception.