继承 QWidget 的类的 mousePressEvent 没响应?
继承 QWidget 的类的 mousePressEvent 和 mouseReleaseEvent 没响应?
AHostList.h
class AHostList:public QWidget
{
Q_OBJECT
public:
AHostList(QString & path);
~AHostList();
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};
AHostList.cpp:
AHostList::AHostList(QString &path)
{
this->setCursor(Qt::BlankCursor);
setWindowFlags(Qt::FramelessWindowHint);
m_mainWidget = new QListWidget(this);
char * pFirstText = "NAME SIZE TIME";
m_firstLabel = new QLabel(this);
m_firstLabel->setFont(QFont("Helvetica",14, QFont::Bold,0));
m_firstLabel->setStyleSheet("QLabel{background-color:blue}");
m_firstLabel->setText(pFirstText);
m_firstLabel->resize(800,35);
QDir picdir(path);
fileList = picdir.entryInfoList();
QFileInfo fileInfo;
QString fileText;
m_sumItem = 0;
m_itemCount = (int)fileList.count();
//从2开始是因为有 . ..
for(int i = 2; i < m_itemCount; i++){
fileInfo = fileList.at(i);
fileText += fileInfo.fileName();
fileText += " ";
fileText += QString::number(fileInfo.size());
fileText += "K ";
QString format("MM-dd hh:mm:ss");
//最后修改的时间
fileText += fileInfo.lastModified().toString(format);
pListWidgetItem[i-2] = new QListWidgetItem(fileText,m_mainWidget);
pListWidgetItem[i-2]->setSizeHint(QSize(800,40));
m_mainWidget->addItem(pListWidgetItem[i-2]);
m_sumItem++;
if(i >= 51)
break;
fileText = "";
}
m_mainWidget->setCurrentRow(0);
m_mainWidget->resize(800,440);
m_mainWidget->move(2,40);
m_mainWidget->setFocus();
show();
}
void AHostList::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"!!!!!!!!!!";
//QWidget::mousePressEvent(event);
}
void AHostList::mouseReleaseEvent(QMouseEvent *event)
{
qDebug()<<"||||||||";
}
没有任何打印信息,但是手指随触摸屏移动时列表可以随之移动
qt?QWidget 类 继承 Qt QWidget
[解决办法]
这几个mouseEvent是protected的虚函数,在QtCreator里是斜体字。
一定要点在没有控件的地方才会有效果。
[解决办法]