小编IAm*_*PLS的帖子

正确关闭QCoreApplication的方法

我正在制作一个Qt5 QCoreApplication,用于从FTP服务器下载文件(从HTTP开始,现在已经切换).

我的程序要关闭时遇到问题.之后,我加入exit(0)downloader.cpp我的计划现已结束,但我得到了以下错误:

QWaitCondition:线程仍在等待时被破坏.

我的代码如下:

main.cpp

#include <QCoreApplication>
#include <downloader.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Downloader d;
    d.doDownload();

    a.exec();
}


**downloader.cpp**

#include "downloader.h"

Downloader::Downloader(QObject *parent) :
    QObject(parent)
{
}

void Downloader::doDownload() {

manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("ftp://ftp.fao.org/Public/GIEWS/windisp/40manual/wd4en.pdf")));
}


void Downloader::replyFinished (QNetworkReply *reply)
{

    if(reply->error()) {
        qDebug() << "ERROR!";
        qDebug() << reply->errorString();
    }
    else

    {
        qDebug() << "Download finished!";

        QFile *file = new QFile("C:/Users/jelicicm/Desktop/wd4en.pdf");

        if(file->open(QFile::Append))
        {
            file->write(reply->readAll());
            file->flush(); file->close(); …
Run Code Online (Sandbox Code Playgroud)

ftp qt download qt5 qcoreapplication

2
推荐指数
1
解决办法
7104
查看次数

是否可以在QVariant中存储QPushButton?

我想知道是否可以在QVariant中存储QPushButton.更确切地说,我试图在具有该功能的QStandardItemModel中使用它setData.这就是我想要做的事情:

QPushButton* button = new QPushButton("Update");
setData(index(0, 0), "Button");
setData(index(0, 1), button);
Run Code Online (Sandbox Code Playgroud)

但显然,它不能像那样工作,所以我尝试了这个:

QVariant variant;
variant.setValue(button);
setData(index(0, 1), QVariant::fromValue(variant));
Run Code Online (Sandbox Code Playgroud)

它也不起作用.我想在不使用QTableView的情况下这样做(我知道在这种视图中有一个setIndexWidget).

提前致谢!

qt qvariant qpushbutton

2
推荐指数
1
解决办法
1128
查看次数

获取相对于 QTreeWidget 上的项目的放置位置

我有一个自定义的 QTreeWidget 类,其dropEvent()方法被重写。方法如下:

void QCustomTreeWidget::dropEvent(QDropEvent * event)
{
    QModelIndex droppedIndex = indexAt(event->pos());

    if (!droppedIndex.isValid())
        return;

    // other logic

    QTreeWidget::dropEvent(event);
}
Run Code Online (Sandbox Code Playgroud)

如何确定该项目是否将插入到其所放置的项目的上方、内部或下方?

qt drag-and-drop qtreewidget qtreewidgetitem

2
推荐指数
1
解决办法
2333
查看次数

SNS通知调度

是否可以通过我们能够解析或参与的 SNS(在一天中的特定时间发送)安排我们的推送通知?

amazon-sns parse-platform moengage

2
推荐指数
1
解决办法
5671
查看次数

如何知道在Qt中单击了哪个按钮?

我的表单中有8个按钮(btn1,btn2,btn3,...,btn8)。每个按钮的文本均来自数据库。我在数据库中定义了8个函数。这些是我的领域:

ID, Text, Active  
Run Code Online (Sandbox Code Playgroud)

当用户按下每个按钮时,我应该将函数名称保存在数据库中。首先,所有按钮都被隐藏。在页面加载中,我从数据库读取数据以显示功能按钮的文本;如果功能按钮未激活,则该按钮也不可见。

这是我的代码:

ui->btn1->hide();
ui->btn2->hide();
ui->btn3->hide();
ui->btn4->hide();
ui->btn5->hide();
ui->btn6->hide();
ui->btn7->hide();
ui->btn8->hide();
 QSqlQueryModel *lst=database->getFunctions();
 QString st;

 QStringList btnlst;
 for(int i = 0; i < lst->rowCount(); ++i)
 {
     if(lst->record(i).value(2).toInt()==1)//ACTIVE
     {
         btnlst<<(lst->record(i).value(3).toString());//text
     }
 }
 for(int i = 0; i < btnlst.count(); ++i)
 {
     QPushButton *btn=this->findChild<QPushButton>(st.append(QString::number(i)));
     btn->setText(btnlst[i]);
     btn->show();
     connect(btn,SIGNAL(clicked()),this,SLOT(Function()));
 }  
Run Code Online (Sandbox Code Playgroud)

在该代码中,我将所有活动函数保存在列表中,然后获取列表计数。例如,如果列表的长度为3,则btn1btn2btn3应该以我的形式显示,而其他应保持隐藏状态。然后,我将所有按钮clicked()信号连接到名为的插槽Function()

我想使用用户在表单中按下的按钮的文本。如何查找单击了哪个按钮,以及如何获取该按钮的文本?

qt

2
推荐指数
1
解决办法
3467
查看次数

我可以使用简单的 html 使用单行大小的 QTextEdit 吗?

我需要显示包含以下样式的文本的简单状态行:

  • 颜色
  • 胆大
  • 斜体

QTextEdit可以渲染简单的 HTML。但它也强行扩展到多行:

图片说明

添加了红色背景以强调QTextEdit. 所需大小是一行文本的大小。我如何做到这一点?

qt qt-designer qtextedit

2
推荐指数
1
解决办法
4813
查看次数

如何检查我的editText字段是否为空并使用Button进入新活动?

我已经制作了这段代码:

public class MainActivity extends AppCompatActivity {

private ImageButton ourButton;
private EditText operand1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Find Views by IDs :
    ourButton = (ImageButton) findViewById(R.id.gobutton);
    operand1 = (EditText) findViewById(R.id.edit_Name);

    String ed_text = operand1.getText().toString().trim();
    if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
    {
        ourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
            }
        });
    }
    else
    {
        ourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View …
Run Code Online (Sandbox Code Playgroud)

java android

1
推荐指数
1
解决办法
82
查看次数

Nativescript:如何扩展ObservableArray?

为什么我会收到以下错误?

通用类型'ObservableArray需要1个类型参数

这是我的代码:

import {ObservableArray} from 'data/observable-array';

export class BaseCollection<T> extends ObservableArray {
}
Run Code Online (Sandbox Code Playgroud)

typescript nativescript

1
推荐指数
1
解决办法
372
查看次数

如何使用QMediaPlayer保存帧?

我想保存一个帧的图像QMediaPlayer.阅读文档后,我明白我应该使用QVideoProbe.我使用以下代码:

QMediaPlayer *player = new QMediaPlayer();
QVideoProbe *probe   = new QVideoProbe;

connect(probe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(processFrame(QVideoFrame)));

qDebug()<<probe->setSource(player); // Returns true, hopefully.

player->setVideoOutput(myVideoSurface);
player->setMedia(QUrl::fromLocalFile("observation.mp4"));
player->play(); // Start receving frames as they get presented to myVideoSurface
Run Code Online (Sandbox Code Playgroud)

但不幸的是,probe->setSource(player)总是false为我返回,因此我的插槽processFrame不会被触发.

我究竟做错了什么 ?有没有人有一个工作的例子QVideoProbe

qt qmediaplayer

1
推荐指数
1
解决办法
3486
查看次数

Qt函数runJavaScript()不执行JavaScript代码

我正在尝试在Qt中实现网页的显示。我选择使用Qt WebEngine来完成任务。这是我所做的:

  • 编写了一个包含空白表格的示例网页。
  • 仅使用API​​编写JS文件即可在表单内创建单选按钮。

在我的代码中,它看起来像这样:

View = new QWebEngineView(this);
// read the js file using qfile
file.open("path to jsFile");
myJsApi = file.Readall();
View->page()->runjavascript (myjsapi);
View->page()->runjavascript ("createRadioButton(\"button1\");");
Run Code Online (Sandbox Code Playgroud)

我发现该runJavaScript()功能对网页没有影响。我可以在输出窗口中看到该网页,但是我期望的单选按钮不存在。我究竟做错了什么?

html javascript qt qtwebengine

1
推荐指数
1
解决办法
2042
查看次数

Qt中的eventFilter错误

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

    installEventFilter(this);
}

bool Widget::eventFilter(QObject *target, QEvent *event)
{
    if(target == this)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key()==QT::Key_Space)
            {
                ui->label->setText("ok");
                return true;
            }
            // ??? ?? ??.
        }
    }

    return QWidget::eventFilter(target, event);
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

从'QEvent*'类型的static_cast无效到类型'QKeyEvent*'

无效使用不完整类型'struct QKeyEvent'

'struct QKeyEvent'的前向声明

我想要的是:

label setText("ok")  keypress Event 'a' in Form
Run Code Online (Sandbox Code Playgroud)

c++ qt

0
推荐指数
1
解决办法
899
查看次数

如何在QWidget中获取样式表更改事件?

我正在尝试为所有小部件读取全局样式表并将其应用于QApplication实例.

这样可以正确地设置所有小部件的样式,除了我无法在主窗口构造函数及其子窗口小部件的构造函数中查询样式选项(如字体和字体大小),因为此时样式表尚未应用于它.

所以我需要:

  • 要么在主窗口构造函数中使全局样式表可用;
  • 应用样式表时捕获窗口小部件中的事件.

有没有办法实现其中之一?

我的主窗口代码如下:

int main(int argc, char **argv)
{   
    QWSServer::QWSServer::setBackground(QBrush(QColor(0, 0, 0, 255)));
    QApplication app(argc, argv);

    QFile stylesheet("/usr/bin/app.qss");
    stylesheet.open(QFile::ReadOnly|QFile::Text);
    QTextStream styleSheetStyle(&stylesheet);

    app.setStyleSheet(styleSheetStyle.readAll());

    MainWindow * pWindow = new MainWindow();
    pWindow->setWindowFlags(Qt::FramelessWindowHint);
    pWindow->show();

    return app.exec();  
}
Run Code Online (Sandbox Code Playgroud)

在小部件中,需要样式:

void paintText(QPixmap *target, const QString &text)
{
    QPainter painter(target);
    painter.setPen(QColor(184,188,193,255));
    painter.setFont(property("font").value<QFont>());
    style()->drawItemText(&painter, 
                         target->rect().adjusted(0,0,0,-15), 
                         Qt::AlignHCenter|Qt::AlignBottom, 
                         QPalette(QColor(184,188,193,255)), 
                         true, 
                         text);
    painter.end();
}
Run Code Online (Sandbox Code Playgroud)

如果在窗口小部件的构造函数中调用该绘制函数,则font是默认的,如果在show事件中调用,则font是全局样式表指定的字体.

但是这个函数只需要调用一次,所以我不想在show事件中绘制它,即使我可以使用一个标志使它只在第一个show事件上运行.

c++ qt

0
推荐指数
1
解决办法
1375
查看次数

如何将对象/指针正确存储到Qlist中

我是Qt的新手,我遇到过这一行:

    QList<Class*>* _lstChildren = new QList<Class*>(); 
Run Code Online (Sandbox Code Playgroud)

其中类派生自另一个类,并且其中包含各种构造函数.

我很好奇:为什么额外*使用的,什么是()在最后的手段?

c++ qt pointers object qlist

-2
推荐指数
1
解决办法
406
查看次数