PDA

View Full Version : Unexpected Button appearing on runtime



mat_de_b
27th July 2015, 14:02
On running my program a button is appearing that I don't believe I have created. It's label-less and produces no action when pressed, so I am unable to find anyway to debug it out.

Here is the window from the Form Editor preview:
11296

Here is the window from the active application, notice the button in the top left corner:
11297

The program works by having a wizard, which contains set up information for the array of buttons on the right of the main window. In order to easily access the buttons I put a pointer to each in an array called ButtonArray[x]. This is the only mention of a QPushButton in my code and so I'm assuming that there must be something wrong with this section? SortButtons are a type that I created to hold button data in strings, nothing to do with actual PushButtons.




QPushButton *ButtonArray[10]; // FROM HEADER

for(int i=0; i <10 ; i++)
{
ButtonArray[i] = new QPushButton(this);
Buttons[i] = new SortButton;
}

ButtonArray[0] = ui->Button1;
ButtonArray[1] = ui->Button2;
ButtonArray[2] = ui->Button3;
ButtonArray[3] = ui->Button4;
ButtonArray[4] = ui->Button5;
ButtonArray[5] = ui->Button6;
ButtonArray[6] = ui->Button7;
ButtonArray[7] = ui->Button8;
ButtonArray[8] = ui->Button9;
ButtonArray[9] = ui->Button10;



Here is the full Mainwindow Code:

Header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileSystemModel>
#include <QTreeView>
#include <QTableView>
#include <QMediaPlayer>
#include <QWidget>
#include <QTWidgets>

//QFileSystemModel *FolderList;
//QTreeView *FolderTree;
//QFileSystemModel *FileList;
//QTableView *FileTree;

#include <QSignalMapper>



QT_BEGIN_NAMESPACE
class QAbstractItemView;
class QLabel;
class QMediaPlayer;
class QModelIndex;
class QPushButton;
class QSlider;
class QVideoProbe;
class QVideoWidget;
QT_END_NAMESPACE


class SortButton{

public:
QString Name;
QKeySequence Hotkey;
QString Folder;

};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;

QFileSystemModel *FolderList;
QFileSystemModel *FileList;

QMediaPlayer *MediaPlayer;
QMediaPlayer *player;
QPushButton *ButtonArray[10];

QString SampleRoot;
bool CopyMove;

void SetButtons(void);
SortButton *Buttons[10];

QFile File;

QSignalMapper* signalMapper;

public slots:
void display();
void LoadButton(QString Fold, QKeySequence Hot, QString Nam, int Array);
void LoadRootInfo(QString Root, bool Copy);
void FileMoves(int Array);
void About();
//void Exit();

private slots:
void on_FolderTreeView_clicked(const QModelIndex &index);

void on_FolderTreeView_activated(const QModelIndex &index);

void on_FileTableView_activated(const QModelIndex &index);


void on_StopButton_clicked();

void on_PlayButton_clicked();

void on_NextButton_clicked();

void on_PrevButton_clicked();



private:


};



#endif // MAINWINDOW_H


Main:

#include <QApplication>
#include <QtWidgets>
#include <QFileSystemModel>
#include <QTreeView>
#include <QTableView>
#include <mainwindow.h>
#include <wizard.h>
#include <QMediaPlayer>


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//app.addLibraryPath(app.applicationDirPath()+"\\"+"libs");

Wizard WizardMenu;

WizardMenu.show();

MainWindow mainWin;


QObject::connect(&WizardMenu,SIGNAL(accepted()),&mainWin,SLOT(display()));

//Map Wizard
QObject::connect(&WizardMenu,&Wizard::SortTypeInfo,&mainWin,&MainWindow::LoadButton);
QObject::connect(&WizardMenu,&Wizard::FirstPageInfo,&mainWin,&MainWindow::LoadRootInfo);
return app.exec();
}

Will put the source in the next post as it is too long

Source:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
//#include <QtMultimedia/QMediaMetaData>
#include <QtWidgets>
#include <QWidget>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{



ui->setupUi(this);

for(int i=0; i <10 ; i++)
{
ButtonArray[i] = new QPushButton(this);
Buttons[i] = new SortButton;
}

ButtonArray[0] = ui->Button1;
ButtonArray[1] = ui->Button2;
ButtonArray[2] = ui->Button3;
ButtonArray[3] = ui->Button4;
ButtonArray[4] = ui->Button5;
ButtonArray[5] = ui->Button6;
ButtonArray[6] = ui->Button7;
ButtonArray[7] = ui->Button8;
ButtonArray[8] = ui->Button9;
ButtonArray[9] = ui->Button10;

signalMapper = new QSignalMapper;

for(int i = 0; i < 10; i++)
{
connect(ButtonArray[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ButtonArray[i], i);
//ButtonArray[i]->setToolTip(QString::number(i));
}

connect(signalMapper, SIGNAL(mapped(int)),
this, SLOT(FileMoves(int)));

FolderList = new QFileSystemModel(this);
FolderList->setRootPath("");
FolderList->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);


ui->FolderTreeView->setModel(FolderList);
ui->FolderTreeView->hideColumn(1);
ui->FolderTreeView->hideColumn(2);
ui->FolderTreeView->hideColumn(3);

FileList = new QFileSystemModel(this);
FileList->setRootPath("");
FileList->setFilter(QDir::NoDotAndDotDot | QDir::Files);
FileList->setNameFilters(QStringList() << "*.mp3" << "*.wav");
FileList->setNameFilterDisables(false);
ui->FileTableView->setModel(FileList);
ui->FileTableView->setRootIndex(FileList->setRootPath("C:\\"));
ui->FileTableView->setColumnWidth(0,200);
ui->FileTableView->setColumnWidth(2,60);
//ui->FileTreeView->setModel(FileList2);

MediaPlayer = new QMediaPlayer(this);

/*QString Error2 = MediaPlayer->errorString();

QMessageBox msgBox3;
msgBox3.setText(Error2);
msgBox3.exec();*/
// connect(MediaPlayer, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
// connect(MediaPlayer, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
MediaPlayer->setVolume(100);

//SetButtons();

// ui->FolderTreeView->hide();
// ui->FileTableView->hide();


connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(About()));
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::About(){
QMessageBox AboutBox;
AboutBox.setText("Holy shit balls");
AboutBox.exec();

}


void MainWindow::on_FolderTreeView_clicked(const QModelIndex &index)
{
QString FolderPath = FolderList->fileInfo(index).absoluteFilePath();
ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
}

void MainWindow::on_FolderTreeView_activated(const QModelIndex &index)
{
QString FolderPath = FolderList->fileInfo(index).absoluteFilePath();
ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
}

void MainWindow::on_FileTableView_activated(const QModelIndex &index)
{
MediaPlayer->stop();
QString FilePath = FileList->fileInfo(index).absoluteFilePath();
MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));

MediaPlayer->play();

/*
QString Error = MediaPlayer->errorString();

QMessageBox msgBox;
msgBox.setText(Error);
msgBox.exec();

QMessageBox msgBox2;

QString Available = "false";
QString AudioA = "false";
QString Muted = "false";
if(MediaPlayer->isAvailable())
Available = "true";
if(MediaPlayer->isAudioAvailable())
AudioA = "true";
if(MediaPlayer->isAvailable())
Muted = "true";

msgBox2.setText(Available + AudioA + Muted);
msgBox2.exec();
*/

}


void MainWindow::SetButtons(void){
QString TempText;

for(int i = 0; i < 10; i++)
{
if (Buttons[i]->Name == "" || Buttons[i]->Folder == "" )
{
ButtonArray[i]->setText("");
ButtonArray[i]->setEnabled(false);
} else {
TempText = Buttons[i]->Name + "\n" + "(&" +Buttons[i]->Hotkey.toString()+")";
ButtonArray[i]->setText(TempText);
ButtonArray[i]->setEnabled(true);
ButtonArray[i]->setShortcut(Buttons[i]->Hotkey);
}
}

//ButtonArray[9]->setEnabled(false);

}

void MainWindow::LoadRootInfo(QString Root, bool Copy)
{
SampleRoot = Root;
CopyMove = Copy;

QModelIndex TempI = FolderList->index(SampleRoot,0);
ui->FolderTreeView->setCurrentIndex(TempI);
QString FolderPath = FolderList->fileInfo(TempI).absoluteFilePath();
ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
}

void MainWindow::FileMoves(int Array)
{

MediaPlayer->stop();
QModelIndex TempIndex;
TempIndex = ui->FileTableView->currentIndex();
QString FileName = FileList->fileInfo(TempIndex).fileName();
if(FileName != "" && FileList->isDir(TempIndex) == false)
{
QString OriginalFile = FileList->fileInfo(TempIndex).absoluteFilePath();
QString FinalFile = FileName;
FinalFile.prepend(Buttons[Array]->Folder+"\\");
if(CopyMove == false)
{
File.copy(OriginalFile,FinalFile);
} else {
File.rename(OriginalFile,FinalFile);
}
}


//Play Next
QModelIndex TempIndex2;
QModelIndex TempIndex3;
TempIndex2 = ui->FileTableView->currentIndex();

ui->FileTableView->selectRow(TempIndex2.row() + 1);
ui->FileTableView->setFocus();

TempIndex3 = ui->FileTableView->currentIndex();
if(TempIndex2 != TempIndex3)
{
QString FilePath = FileList->fileInfo(TempIndex3).absoluteFilePath();
MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
MediaPlayer->play();
}
}



void MainWindow::on_StopButton_clicked()
{
MediaPlayer->stop();
}

void MainWindow::on_PlayButton_clicked()
{
MediaPlayer->stop();
QModelIndex TempIndex;
TempIndex = ui->FileTableView->currentIndex();
QString FilePath = FileList->fileInfo(TempIndex).absoluteFilePath();
MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
MediaPlayer->play();
}

void MainWindow::on_NextButton_clicked()
{
MediaPlayer->stop();
QModelIndex TempIndex;
QModelIndex TempIndex2;
TempIndex = ui->FileTableView->currentIndex();

ui->FileTableView->selectRow(TempIndex.row() + 1);
ui->FileTableView->setFocus();

TempIndex2 = ui->FileTableView->currentIndex();
if(TempIndex != TempIndex2)
{
QString FilePath = FileList->fileInfo(TempIndex2).absoluteFilePath();
MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
MediaPlayer->play();
}
}

void MainWindow::on_PrevButton_clicked()
{
MediaPlayer->stop();
QModelIndex TempIndex;
QModelIndex TempIndex2;
TempIndex = ui->FileTableView->currentIndex();

ui->FileTableView->selectRow(TempIndex.row() - 1);
ui->FileTableView->setFocus();

TempIndex2 = ui->FileTableView->currentIndex();
if(TempIndex != TempIndex2)
{
QString FilePath = FileList->fileInfo(TempIndex2).absoluteFilePath();
MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
MediaPlayer->play();
}
}

void MainWindow::display()
{
showMaximized();
SetButtons();
}

void MainWindow::LoadButton(QString Fold, QKeySequence Hot, QString Nam, int Array)
{
Buttons[Array]->Folder = Fold;
Buttons[Array]->Hotkey = Hot;
Buttons[Array]->Name = Nam;
}

anda_skoa
27th July 2015, 14:44
So you first create 10 QPushButtons in a loop and then "forget" about them by overwriting their pointers in the array with buttons from the ui structure?

Cheers,
_

mat_de_b
27th July 2015, 14:49
Ah I understand now, removed the reintialisation and the unexpected button is gone. Not sure why I thought that step was necessary, thanks.