Results 1 to 3 of 3

Thread: Unexpected Button appearing on runtime

  1. #1
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unexpected Button appearing on runtime

    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:
    NoButton.jpg

    Here is the window from the active application, notice the button in the top left corner:
    Button.jpg

    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.

    Qt Code:
    1. QPushButton *ButtonArray[10]; // FROM HEADER
    2.  
    3. for(int i=0; i <10 ; i++)
    4. {
    5. ButtonArray[i] = new QPushButton(this);
    6. Buttons[i] = new SortButton;
    7. }
    8.  
    9. ButtonArray[0] = ui->Button1;
    10. ButtonArray[1] = ui->Button2;
    11. ButtonArray[2] = ui->Button3;
    12. ButtonArray[3] = ui->Button4;
    13. ButtonArray[4] = ui->Button5;
    14. ButtonArray[5] = ui->Button6;
    15. ButtonArray[6] = ui->Button7;
    16. ButtonArray[7] = ui->Button8;
    17. ButtonArray[8] = ui->Button9;
    18. ButtonArray[9] = ui->Button10;
    To copy to clipboard, switch view to plain text mode 


    Here is the full Mainwindow Code:

    Header:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFileSystemModel>
    6. #include <QTreeView>
    7. #include <QTableView>
    8. #include <QMediaPlayer>
    9. #include <QWidget>
    10. #include <QTWidgets>
    11.  
    12. //QFileSystemModel *FolderList;
    13. //QTreeView *FolderTree;
    14. //QFileSystemModel *FileList;
    15. //QTableView *FileTree;
    16.  
    17. #include <QSignalMapper>
    18.  
    19.  
    20.  
    21. QT_BEGIN_NAMESPACE
    22. class QLabel;
    23. class QMediaPlayer;
    24. class QSlider;
    25. class QVideoProbe;
    26. class QVideoWidget;
    27. QT_END_NAMESPACE
    28.  
    29.  
    30. class SortButton{
    31.  
    32. public:
    33. QString Name;
    34. QKeySequence Hotkey;
    35. QString Folder;
    36.  
    37. };
    38.  
    39. namespace Ui {
    40. class MainWindow;
    41. }
    42.  
    43. class MainWindow : public QMainWindow
    44. {
    45. Q_OBJECT
    46.  
    47. public:
    48. explicit MainWindow(QWidget *parent = 0);
    49. ~MainWindow();
    50. Ui::MainWindow *ui;
    51.  
    52. QFileSystemModel *FolderList;
    53. QFileSystemModel *FileList;
    54.  
    55. QMediaPlayer *MediaPlayer;
    56. QMediaPlayer *player;
    57. QPushButton *ButtonArray[10];
    58.  
    59. QString SampleRoot;
    60. bool CopyMove;
    61.  
    62. void SetButtons(void);
    63. SortButton *Buttons[10];
    64.  
    65. QFile File;
    66.  
    67. QSignalMapper* signalMapper;
    68.  
    69. public slots:
    70. void display();
    71. void LoadButton(QString Fold, QKeySequence Hot, QString Nam, int Array);
    72. void LoadRootInfo(QString Root, bool Copy);
    73. void FileMoves(int Array);
    74. void About();
    75. //void Exit();
    76.  
    77. private slots:
    78. void on_FolderTreeView_clicked(const QModelIndex &index);
    79.  
    80. void on_FolderTreeView_activated(const QModelIndex &index);
    81.  
    82. void on_FileTableView_activated(const QModelIndex &index);
    83.  
    84.  
    85. void on_StopButton_clicked();
    86.  
    87. void on_PlayButton_clicked();
    88.  
    89. void on_NextButton_clicked();
    90.  
    91. void on_PrevButton_clicked();
    92.  
    93.  
    94.  
    95. private:
    96.  
    97.  
    98. };
    99.  
    100.  
    101.  
    102. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Main:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtWidgets>
    3. #include <QFileSystemModel>
    4. #include <QTreeView>
    5. #include <QTableView>
    6. #include <mainwindow.h>
    7. #include <wizard.h>
    8. #include <QMediaPlayer>
    9.  
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication app(argc, argv);
    14. //app.addLibraryPath(app.applicationDirPath()+"\\"+"libs");
    15.  
    16. Wizard WizardMenu;
    17.  
    18. WizardMenu.show();
    19.  
    20. MainWindow mainWin;
    21.  
    22.  
    23. QObject::connect(&WizardMenu,SIGNAL(accepted()),&mainWin,SLOT(display()));
    24.  
    25. //Map Wizard
    26. QObject::connect(&WizardMenu,&Wizard::SortTypeInfo,&mainWin,&MainWindow::LoadButton);
    27. QObject::connect(&WizardMenu,&Wizard::FirstPageInfo,&mainWin,&MainWindow::LoadRootInfo);
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

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

    Source:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMediaPlayer>
    4. //#include <QtMultimedia/QMediaMetaData>
    5. #include <QtWidgets>
    6. #include <QWidget>
    7. #include <QMessageBox>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13.  
    14.  
    15.  
    16. ui->setupUi(this);
    17.  
    18. for(int i=0; i <10 ; i++)
    19. {
    20. ButtonArray[i] = new QPushButton(this);
    21. Buttons[i] = new SortButton;
    22. }
    23.  
    24. ButtonArray[0] = ui->Button1;
    25. ButtonArray[1] = ui->Button2;
    26. ButtonArray[2] = ui->Button3;
    27. ButtonArray[3] = ui->Button4;
    28. ButtonArray[4] = ui->Button5;
    29. ButtonArray[5] = ui->Button6;
    30. ButtonArray[6] = ui->Button7;
    31. ButtonArray[7] = ui->Button8;
    32. ButtonArray[8] = ui->Button9;
    33. ButtonArray[9] = ui->Button10;
    34.  
    35. signalMapper = new QSignalMapper;
    36.  
    37. for(int i = 0; i < 10; i++)
    38. {
    39. connect(ButtonArray[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
    40. signalMapper->setMapping(ButtonArray[i], i);
    41. //ButtonArray[i]->setToolTip(QString::number(i));
    42. }
    43.  
    44. connect(signalMapper, SIGNAL(mapped(int)),
    45. this, SLOT(FileMoves(int)));
    46.  
    47. FolderList = new QFileSystemModel(this);
    48. FolderList->setRootPath("");
    49. FolderList->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    50.  
    51.  
    52. ui->FolderTreeView->setModel(FolderList);
    53. ui->FolderTreeView->hideColumn(1);
    54. ui->FolderTreeView->hideColumn(2);
    55. ui->FolderTreeView->hideColumn(3);
    56.  
    57. FileList = new QFileSystemModel(this);
    58. FileList->setRootPath("");
    59. FileList->setFilter(QDir::NoDotAndDotDot | QDir::Files);
    60. FileList->setNameFilters(QStringList() << "*.mp3" << "*.wav");
    61. FileList->setNameFilterDisables(false);
    62. ui->FileTableView->setModel(FileList);
    63. ui->FileTableView->setRootIndex(FileList->setRootPath("C:\\"));
    64. ui->FileTableView->setColumnWidth(0,200);
    65. ui->FileTableView->setColumnWidth(2,60);
    66. //ui->FileTreeView->setModel(FileList2);
    67.  
    68. MediaPlayer = new QMediaPlayer(this);
    69.  
    70. /*QString Error2 = MediaPlayer->errorString();
    71.  
    72.   QMessageBox msgBox3;
    73.   msgBox3.setText(Error2);
    74.   msgBox3.exec();*/
    75. // connect(MediaPlayer, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    76. // connect(MediaPlayer, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
    77. MediaPlayer->setVolume(100);
    78.  
    79. //SetButtons();
    80.  
    81. // ui->FolderTreeView->hide();
    82. // ui->FileTableView->hide();
    83.  
    84.  
    85. connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(About()));
    86. connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
    87. }
    88.  
    89. MainWindow::~MainWindow()
    90. {
    91. delete ui;
    92. }
    93.  
    94. void MainWindow::About(){
    95. QMessageBox AboutBox;
    96. AboutBox.setText("Holy shit balls");
    97. AboutBox.exec();
    98.  
    99. }
    100.  
    101.  
    102. void MainWindow::on_FolderTreeView_clicked(const QModelIndex &index)
    103. {
    104. QString FolderPath = FolderList->fileInfo(index).absoluteFilePath();
    105. ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
    106. }
    107.  
    108. void MainWindow::on_FolderTreeView_activated(const QModelIndex &index)
    109. {
    110. QString FolderPath = FolderList->fileInfo(index).absoluteFilePath();
    111. ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
    112. }
    113.  
    114. void MainWindow::on_FileTableView_activated(const QModelIndex &index)
    115. {
    116. MediaPlayer->stop();
    117. QString FilePath = FileList->fileInfo(index).absoluteFilePath();
    118. MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
    119.  
    120. MediaPlayer->play();
    121.  
    122. /*
    123.   QString Error = MediaPlayer->errorString();
    124.  
    125.   QMessageBox msgBox;
    126.   msgBox.setText(Error);
    127.   msgBox.exec();
    128.  
    129.   QMessageBox msgBox2;
    130.  
    131.   QString Available = "false";
    132.   QString AudioA = "false";
    133.   QString Muted = "false";
    134.   if(MediaPlayer->isAvailable())
    135.   Available = "true";
    136.   if(MediaPlayer->isAudioAvailable())
    137.   AudioA = "true";
    138.   if(MediaPlayer->isAvailable())
    139.   Muted = "true";
    140.  
    141.   msgBox2.setText(Available + AudioA + Muted);
    142.   msgBox2.exec();
    143.   */
    144.  
    145. }
    146.  
    147.  
    148. void MainWindow::SetButtons(void){
    149. QString TempText;
    150.  
    151. for(int i = 0; i < 10; i++)
    152. {
    153. if (Buttons[i]->Name == "" || Buttons[i]->Folder == "" )
    154. {
    155. ButtonArray[i]->setText("");
    156. ButtonArray[i]->setEnabled(false);
    157. } else {
    158. TempText = Buttons[i]->Name + "\n" + "(&" +Buttons[i]->Hotkey.toString()+")";
    159. ButtonArray[i]->setText(TempText);
    160. ButtonArray[i]->setEnabled(true);
    161. ButtonArray[i]->setShortcut(Buttons[i]->Hotkey);
    162. }
    163. }
    164.  
    165. //ButtonArray[9]->setEnabled(false);
    166.  
    167. }
    168.  
    169. void MainWindow::LoadRootInfo(QString Root, bool Copy)
    170. {
    171. SampleRoot = Root;
    172. CopyMove = Copy;
    173.  
    174. QModelIndex TempI = FolderList->index(SampleRoot,0);
    175. ui->FolderTreeView->setCurrentIndex(TempI);
    176. QString FolderPath = FolderList->fileInfo(TempI).absoluteFilePath();
    177. ui->FileTableView->setRootIndex(FileList->setRootPath(FolderPath));
    178. }
    179.  
    180. void MainWindow::FileMoves(int Array)
    181. {
    182.  
    183. MediaPlayer->stop();
    184. QModelIndex TempIndex;
    185. TempIndex = ui->FileTableView->currentIndex();
    186. QString FileName = FileList->fileInfo(TempIndex).fileName();
    187. if(FileName != "" && FileList->isDir(TempIndex) == false)
    188. {
    189. QString OriginalFile = FileList->fileInfo(TempIndex).absoluteFilePath();
    190. QString FinalFile = FileName;
    191. FinalFile.prepend(Buttons[Array]->Folder+"\\");
    192. if(CopyMove == false)
    193. {
    194. File.copy(OriginalFile,FinalFile);
    195. } else {
    196. File.rename(OriginalFile,FinalFile);
    197. }
    198. }
    199.  
    200.  
    201. //Play Next
    202. QModelIndex TempIndex2;
    203. QModelIndex TempIndex3;
    204. TempIndex2 = ui->FileTableView->currentIndex();
    205.  
    206. ui->FileTableView->selectRow(TempIndex2.row() + 1);
    207. ui->FileTableView->setFocus();
    208.  
    209. TempIndex3 = ui->FileTableView->currentIndex();
    210. if(TempIndex2 != TempIndex3)
    211. {
    212. QString FilePath = FileList->fileInfo(TempIndex3).absoluteFilePath();
    213. MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
    214. MediaPlayer->play();
    215. }
    216. }
    217.  
    218.  
    219.  
    220. void MainWindow::on_StopButton_clicked()
    221. {
    222. MediaPlayer->stop();
    223. }
    224.  
    225. void MainWindow::on_PlayButton_clicked()
    226. {
    227. MediaPlayer->stop();
    228. QModelIndex TempIndex;
    229. TempIndex = ui->FileTableView->currentIndex();
    230. QString FilePath = FileList->fileInfo(TempIndex).absoluteFilePath();
    231. MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
    232. MediaPlayer->play();
    233. }
    234.  
    235. void MainWindow::on_NextButton_clicked()
    236. {
    237. MediaPlayer->stop();
    238. QModelIndex TempIndex;
    239. QModelIndex TempIndex2;
    240. TempIndex = ui->FileTableView->currentIndex();
    241.  
    242. ui->FileTableView->selectRow(TempIndex.row() + 1);
    243. ui->FileTableView->setFocus();
    244.  
    245. TempIndex2 = ui->FileTableView->currentIndex();
    246. if(TempIndex != TempIndex2)
    247. {
    248. QString FilePath = FileList->fileInfo(TempIndex2).absoluteFilePath();
    249. MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
    250. MediaPlayer->play();
    251. }
    252. }
    253.  
    254. void MainWindow::on_PrevButton_clicked()
    255. {
    256. MediaPlayer->stop();
    257. QModelIndex TempIndex;
    258. QModelIndex TempIndex2;
    259. TempIndex = ui->FileTableView->currentIndex();
    260.  
    261. ui->FileTableView->selectRow(TempIndex.row() - 1);
    262. ui->FileTableView->setFocus();
    263.  
    264. TempIndex2 = ui->FileTableView->currentIndex();
    265. if(TempIndex != TempIndex2)
    266. {
    267. QString FilePath = FileList->fileInfo(TempIndex2).absoluteFilePath();
    268. MediaPlayer->setMedia(QUrl::fromLocalFile(FilePath));
    269. MediaPlayer->play();
    270. }
    271. }
    272.  
    273. void MainWindow::display()
    274. {
    275. showMaximized();
    276. SetButtons();
    277. }
    278.  
    279. void MainWindow::LoadButton(QString Fold, QKeySequence Hot, QString Nam, int Array)
    280. {
    281. Buttons[Array]->Folder = Fold;
    282. Buttons[Array]->Hotkey = Hot;
    283. Buttons[Array]->Name = Nam;
    284. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unexpected Button appearing on runtime

    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,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unexpected Button appearing on runtime

    Ah I understand now, removed the reintialisation and the unexpected button is gone. Not sure why I thought that step was necessary, thanks.

Similar Threads

  1. Unexpected QTextCursor behaviour
    By Binary91 in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2015, 12:48
  2. Menu bar not appearing
    By GUIman in forum Newbie
    Replies: 5
    Last Post: 23rd February 2011, 07:00
  3. Replies: 1
    Last Post: 25th September 2010, 08:20
  4. qwt plugin not appearing in qt3
    By gilbertd26 in forum Qwt
    Replies: 2
    Last Post: 5th January 2009, 10:39
  5. Replies: 3
    Last Post: 16th November 2006, 12:24

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.