[英]Why does my QGraphicsView not showing up in my MainWindow in Qt4?
[英]Qt - Why can't I trigger mousePressEvent for my custom button in MainWindow
這是我第一次詢問Stack Overflow。 如果有任何關於提問的建議,請告訴我。 我是Qt的新手,在使用事件時遇到了一些問題。 希望有人可以提供任何想法。
背景:
我有我的自定義按鈕,這是rxPushButton
在rx.h
和rx.cpp
。 我使用on_rxPushButton_clicked
來改變圖像,它運行得很好。 在MainWindow
,我需要使用一些rx
所以我包含類rx
,我想檢測是否按下鼠標的左鍵,我需要知道哪個rx
被按下並在MainWindow
int
rxId
中記錄它的id。
問題:
我嘗試了兩種方法來實現我的目標,包括mousePressEvent
和eventFilter
。 我發現我無法檢測到鼠標按下信號上的任何rx
,但我可以檢測到它的外部rx
在其他地方Mainwindow
。 我想知道事件是否會發生沖突,但是當我在rx.cpp
評論on_rxPushButton_clicked
時, MainWindow
仍無法解決問題。 所以我認為rx
占用的屏幕空間可能不會受到MainWindow
控制(我可以在我的代碼中獲得Debug消息“test1”而不是“test2”,請查看下面的內容)。 如果我需要兩件事(在rx
更改圖像並在MainWindow
修改一個變量),我該怎么做才能解決這個問題? 或者也許這只是我的代碼有問題以及如何修改?
我希望盡可能將它們分開,因為我將來還需要在MainWindow
中包含許多對象。
以下是我的一些相關代碼:
rx.cpp
void rx::on_rxPushButton_clicked(void)
{
startLoading();
}
void rx::startLoading(void)
{
state = 1;
connect(timer, SIGNAL(timeout()), this, SLOT(loading1()));
timer->start(LOADING_INTERVAL);
}
void rx::loading1(void)
{
if(state == 1)
{
state = 2;
ui->rxPushButton->setStyleSheet("border-image: url(:/images/Rx/Rxloading1.png);");
disconnect(timer, SIGNAL(timeout()), this, SLOT(loading1()));
}
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i = 0 ; i < rxSize ; i++)
{
rxList << new rx(this);
int j = i/rxHorizontalCount;
rxList[i]->setGeometry(500+(i-j*8)*110,10+j*90,100,90);
}
//rxList[0]->installEventFilter(this);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
qDebug() << "test1";
if(rxList[0]->rect().contains(event->x(), event->y()))
qDebug() << "test2";
}
}
事件傳播會阻止您執行此操作。 當鼠標在Button上時,事件發送到按鈕,而不是MainWindow,這就是為什么你從未在調試輸出中看到test2的原因。
因為我不知道你需要什么,所以很難說你應該做什么。 您可以在Button中處理事件並發送一些信號或其他信息,或者設置事件過濾器並檢查目標對象是否是您的按鈕,依此類推。
無論如何,閱讀Qt事件系統文檔以便更好地理解。
在rx中更改圖像
使用QSignalMapper類捆綁來自QWidgets(類rx)的信號,並使用與發送信號的對象相對應的widget(類rx)參數重新發出它們。 我們將每個按鈕的clicked()信號連接到信號映射器的map()插槽,並在信號映射器中創建從每個按鈕到按鈕本身的映射。 最后,我們將信號映射器的mapped()信號連接到自定義小部件(rx對象)clicked()信號。 當用戶單擊按鈕時,自定義窗口小部件(rx對象)將發出單個clicked()信號,其參數是用戶單擊的按鈕(rx對象)。 處理MainWindow :: slotButtonsClicked(QWidget * p)函數中的按鈕單擊事件。
在MainWindow中修改n變量:
您可以在mousePressEvent函數中更新MainWindow的變量
class MainWindow : public QWidget
{
public :
MainWindow (QWidget *parent);
QSignalMapper * mapper;
slots:
void slotButtonsClicked(QWidget *);
// ...
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mapper = new QSignalMapper(this);
for(int i = 0 ; i < rxSize ; i++)
{
rxList << new rx(this);
int j = i/rxHorizontalCount;
rxList[i]->setGeometry(500+(i-j*8)*110,10+j*90,100,90);
QObject::connect(rxList[i], SIGNAL(clicked()),mapper,SLOT(map()));
//Adds a mapping so that when map() is signalled from the sender, the signal mapped(widget ) is emitted.
mapper->setMapping(rxList[i], rxList[i]);
}
QObject::connect(mapper,SIGNAL(mapped(QWidget *)),this,SLOT(slotButtonsClicked(QWidget *)));
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
qDebug() << "Clicked outside the button";
//Now you can modify n variable in MainWindow
}
MainWindow::mousePressEvent(event);
}
void MainWindow::slotButtonsClicked(QWidget *p)
{
rx *button = dynamic_cast<rx *>(p);
button->on_rxPushButton_clicked();
qDebug() << button <<" clicked on button";
//Now you can change image in rx
}
希望這工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.