Invisible Buttons



  • Hi all,

    i have a problem with my QPushButtons. In Qt Simulator everything works fine,
    but not on my Nokia N8. But I have no idea why. Heres my code:

    mainwindow.h:
    @private:
    Ui::MainWindow *ui;
    QPushButton *btn[5][10];@

    mainwindow.cpp:
    @ for (int i = 0; i < 5; i ++){
    for (int k = 0; k < 10; k ++){
    btn[i][k] = new QPushButton("", ui->widget_2);
    btn[i][k]->installEventFilter(this);
    btn[i][k]->show();
    }
    }
    ui->widget_2->show();@

    On N8 these buttons are not shown, but i can click them.
    Any ideas? Thanks in advance!



  • Maybe because they are not in a layout?



  • what do i need a layout for?
    the buttons are there, but they were not painted, also with ->repaint() nothing happens.
    And in Qt Simulator everything works fine!



  • perhaps they are located "under" other controls, that are placed in the UI?



  • A layout is needed to layout the buttons :-) Otherwise they are painted more or less randomly, may overlap or another widget may be painted on top of them and the like. Also, your buttons have neither text nor an icon.

    Time to read "Layout Management":http://doc.qt.nokia.com/4.7/layout.html in the docs. You will run into serious problems if you do not layout your UI elements (either manually or using Qt's layout classes).



  • thanks for your answers!
    but i don't think that the problem is another object in front of the buttons, because i can click these buttons. a label changes when i click one of these buttons. so i think that there is no paintevent for these buttons, but why? everything else is working fine, and in simulator the buttons are shown. so they are there, but just not painted...

    i can't test at the moment, but i will try it with a layout.



  • Just a question... do you have an eventloop spinning and everyting? Did you create a QApplication object?



  • [quote author="Matze5590" date="1299849226"]thanks for your answers!
    but i don't think that the problem is another object in front of the buttons, because i can click these buttons. a label changes when i click one of these buttons. so i think that there is no paintevent for these buttons, but why? everything else is working fine, and in simulator the buttons are shown. so they are there, but just not painted...

    i can't test at the moment, but i will try it with a layout. [/quote]

    It can be a label in front, disabling the paint, as it paints itself on top. As a label dioes not react on a mouse click, it is send to the next widget, which could be the button...



  • If you show us your code, we can perhaps help a bit more. The best would be, make a small, simple example where the error can be reproduced.



  • ok, i made a small example of my problem:

    main.cpp:
    @#include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    w.showFullScreen();
    
    
    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.h:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QPushButton>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    QPushButton *btn[2][2];

    private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
    bool eventFilter(QObject *obj, QEvent *ev);
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

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

    void MainWindow::on_pushButton_clicked()
    {
    for (int i = 0; i < 2; i ++){
    for (int k = 0; k < 2; k ++){
    btn[i][k] = new QPushButton("", ui->widget);
    btn[i][k]->setGeometry(i * 75 + 80, k * 75 + 200, 75, 75);
    btn[i][k]->installEventFilter(this);
    btn[i][k]->show();
    }
    }
    ui->pushButton_2->setEnabled(true);
    ui->pushButton->setEnabled(false);
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    for (int i = 0; i < 2; i ++){
    for (int k = 0; k < 2; k ++){
    btn[i][k]->~QPushButton();
    }
    }
    ui->pushButton_2->setEnabled(false);
    ui->pushButton->setEnabled(true);
    }
    bool MainWindow::eventFilter(QObject *obj, QEvent *ev)
    {
    for (int i = 0; i < 2; i ++){
    for (int k = 0; k < 2; k ++){
    if (obj == btn[i][k] && ev->type() == QEvent::MouseButtonRelease){
    QString str;
    str.sprintf("%ld, %ld", i, k);
    ui->label->setText(str);
    }
    }
    }
    }
    @

    in simulator i can see the buttons, but not on Nokia N8. But i can click them!



  • If you give the buttons a text, is it visible?

    Are the buttons and text visible if you put them into a layout?

    Some further hints:

    • You must not call the destructor method yourself (line 34)! You must call

    @
    delete btn[i][k];
    @

    • You should avoid QString::sprintf (line 46) and replace it with arg:

    @
    QString str = QString("%1, %2").arg(i).arg(k);
    @

    • Your event filter method lacks a return value.


  • The eventfilter is quite dangerous, I think.

    Question is: why use eventfilters at all? Just connect to the clicked() signal instead, or if you really want an array, use a QSignalMapper.



  • In the eventfilter you have to return true if you consumed the event or false if you ignored the event. It's important to return a value from a method with a return value!



  • wow!! thanks, the retrun value was the reason!!! Thank you so much!!


Log in to reply
 

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