我使用Ubuntu 12.10 64位.我从Ubuntu repo安装了Qt4.版本是Qt 4.8.3和Qt Creator 2.5.2
我还从qt-project.org下载了Qt5,其版本号是Qt 5.0.1和Qt Creator 2.6.2
当我编译并运行相同的项目时,它们的外观略有不同.这是什么原因?是因为Ubuntu还是Qt5?
我需要在qt中查看目录.创建了连接.但是当文件编辑时,发件人发出两次目录更改信号.filechanged信号也同时发出.发射信号两次有什么用?有没有办法知道哪个文件/文件夹被更改?请帮忙..这是连接代码
QFileSystemWatcher *watcher = new QFileSystemWatcher;
watcher->addPath("/home/user/directory/");
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(directoryChangedHandler(QString)));
Run Code Online (Sandbox Code Playgroud)
和功能
void MainWindow::directoryChangedHandler(QString path)
{
QMessageBox::information(0, "Directory Changed", "Directory Changed at " + path);
}
Run Code Online (Sandbox Code Playgroud) Fedora 17 x64上的Qt 4.8
在我QWidget::mouseMoveEvent,我试图检查光标当前是否设置为Qt::SizeVerCursor或Qt::SizeHorCursor,但QCursor通过调用返回QWidget::cursor()无法与任何一个Qt::SizeVerCursor或Qt::SizeHorCursor由于编译错误.看起来这是因为两者Qt::SizeVerCursor并且Qt::SizeHorCursor实际上是Qt::CursorShape代替QCursor.
此代码无法编译:
void MyGraphicsView::mouseMoveEvent( QMouseEvent *event )
{
if( ( cursor() == Qt::SizeHorCursor ) || ( cursor() == Qt::SizeVerCursor ) )
{
qDebugGreen() << "Cursor is a size cursor!";
}
QGraphicsView::mouseMoveEvent( event );
}
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
error: no match for ‘operator==’ in ‘QWidget::cursor() const() == (Qt::CursorShape)6u’
如何检查当前光标要么Qt::SizeVerCursor还是Qt::SizeHorCursor?
我有一个 class SomeObjectList : public QList<SomeObject*>
我正在创建指针SomeObject并将它们粘贴在列表中,如下所示:
SomeObjectList MyClass::getSomeObjects()
{
SomeObjectList list;
for( int i=0; i<10; ++i )
{
list << new SomeObject();
}
return list; // Crashes approximately here in QList::~QList
}
Run Code Online (Sandbox Code Playgroud)
我在QList析构函数中遇到崩溃,看起来可能正在调用free().我找到了该函数的源代码(Qt 4.8.2),它看起来像这样:
template <typename T>
Q_OUTOFLINE_TEMPLATE QList<T>::~QList()
{
if (!d->ref.deref())
free(d);
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着free()在调用析构函数时调用列表中的任何指针?我在其他地方使用过列表没有这个问题.在什么情况下析构函数会调用free()?
我想写一系列字节给a QDataStream.在十六进制编辑器中查看时,我希望生成的文件如下所示:
0x DEAD
Run Code Online (Sandbox Code Playgroud)
我的尝试看起来像这样:
QFile file("test.txt");
file.open(QIODevice::WriteOnly);
QDataStream stream(&file);
stream << ((char)0xDE);
stream << ((char)0xAD);
file.close();
Run Code Online (Sandbox Code Playgroud)
当我test.txt用十六进制编辑器打开时,我可以看到,而不是将它们写为单个字节,DataStream已将它们填充为全字,文件如下所示:
0x FFFF FFDE FFFF FFAD
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我如何使用foreach循环QSignalSpy?
这就是我想要做的:
foreach( const QList<QVariant> &args, mySignalSpy )
{
Q_ASSERT( args.count() == 2 );
QVariant arg0 = args[0];
QVariant arg1 = args[1];
doSomething( arg0, arg1 );
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
/usr/local/Trolltech/Qt-4.8.2-shared/include/QtTest/qsignalspy.h: In instantiation of ‘QForeachContainer<T>::QForeachContainer(const T&) [with T = QSignalSpy]’:
MyTester.cxx:843:64: required from here
/usr/local/Trolltech/Qt-4.8.2-shared/include/QtCore/qobject.h:333:5: error: ‘QObject::QObject(const QObject&)’ is private
Run Code Online (Sandbox Code Playgroud) 有没有办法在QT按钮的文本(一个字母)中使用下划线是否可行?
我想创建一个按钮,通过单击事件和键盘快捷键触发,并使我想让下划线文本的一个字母更快地显示快捷方式.
提前致谢,对不起我的英语.
我在Qt 4.7工作,我有一个完全填充的QTableView,其中包含可变行数和设置列数.我有一个QStringList,我需要填充特定列的值,让我们说每行的第二个.到目前为止我有这个:
QStringList list;
for (int i = 0; i < ui->myTableView->height()/*see note below*/; i++)
{
list.append(/*code I still need...*/);
}
Run Code Online (Sandbox Code Playgroud)
//note: I also tried it using this->ui->myTableView->model->rowCount(), not sure which is best for this...
QStringList list;
for (int i = 0; i < ui->myTableView->height()/*see note below*/; i++)
{
list.append(/*code I still need...*/);
}
Run Code Online (Sandbox Code Playgroud)
//note: I also tried it using this->ui->myTableView->model->rowCount(), not sure which is best for this...
我的问题是我找不到任何能让我在给定行号和列号的情况下获取QTableView值的函数.我不知道我是不是只是忽略它,但我一直在浏览文档,似乎无法找到任何东西.如果有人有任何想法,我真的很感激帮助.谢谢!
我试图使用Qt4.8文档中给出的示例:
#include <QCoreApplication>
#include <QString>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString str;
str = "%1 %2";
str.arg("%1f", "Hello"); // returns "%1f Hello"
std::cout << str.toStdString().c_str() << std::endl;
str.arg("%1f").arg("Hello"); // returns "Hellof %2"
std::cout << str.toStdString().c_str() << std::endl;
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
然而,这输出:
%1 %2
%1 %2
两次.我在Windows 7和Ubuntu上尝试过这个,使用QtCreator和命令行.我检查过了
QMake version 2.01a
Using Qt version 4.8.1 in /usr/lib/x86_64-linux-gnu
在Windows中:
QMake version 2.01a
Using Qt version 4.7.0 in C:\qt\4.7.0\lib
我甚至检查了我的源文件中的非ascii字符,例如"%"符号是正确的.请告诉我为什么这不起作用!
这是我正在使用的PRO文件:
QT += core
QT -= gui …Run Code Online (Sandbox Code Playgroud) 我打算制作一个非矩形QDialog,所以我通过覆盖绘制一个png图像.QDialog::paintEvent除了在对话框中显示一个灰色边框外,一切都没问题.就像这样:

我确信边框不属于图像,我曾经setWindowFlags(Qt::FramelessWindowHint)和setAttribute(Qt::WA_TranslucentBackground, true).我试图设置qss border-width: 0px但是没有用.有没有办法删除边框?为什么显示它?
qt4 ×10
qt ×9
c++ ×7
borderless ×1
cursor ×1
destructor ×1
foreach ×1
io ×1
qlist ×1
qstring ×1
qt5 ×1
qtableview ×1
qtestlib ×1
qwidget ×1
stylesheet ×1
transparent ×1